Part Number Hot Search : 
MC10181L EC734606 5TRPB 6050069 PC5004 CXA3810M MTDS535F C1H10
Product Description
Full Text Search
 

To Download 18F4539 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  ? 2002 microchip technology inc. preliminary ds30485a pic18fxx39 data sheet enhanced flash microcontrollers with single phase induction motor control kernel
ds30485a - page ii preliminary ? 2002 microchip technology inc. information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. it is your responsibility to ensure that your application meets with your specifications. no representation or warranty is given and no liability is assumed by microchip technology incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. use of microchip?s products as critical com- ponents in life support systems is not authorized except with express written approval by microchip. no licenses are con- veyed, implicitly or otherwise, under any intellectual property rights. trademarks the microchip name and logo, the microchip logo, k ee l oq , mplab, pic, picmicro, picstart and pro mate are registered trademarks of microchip technology incorporated in the u.s.a. and other countries. filterlab, micro id , mxdev, mxlab, picmaster, seeval and the embedded control solutions company are registered trademarks of microchip technology incorporated in the u.s.a. dspic, dspicdem.net, economonitor, fansense, flexrom, fuzzylab, in-circuit serial programming, icsp, icepic, microport, migratable memory, mpasm, mplib, mplink, mpsim, picc, picdem, picdem.net, rfpic, select mode and total endurance are trademarks of microchip technology incorporated in the u.s.a. and other countries. serialized quick turn programming (sqtp) is a service mark of microchip technology incorporated in the u.s.a. all other trademarks mentioned herein are property of their respective companies. ? 2002, microchip technology incorporated, printed in the u.s.a., all rights reserved. printed on recycled paper. microchip received qs-9000 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in chandler and tempe, arizona in july 1999 and mountain view, california in march 2002. the company?s quality system processes and procedures are qs-9000 compliant for its picmicro ? 8-bit mcus, k ee l oq ? code hopping devices, serial eeproms, microperipherals, non-volatile memory and analog products. in addition, microchip?s quality system for the design and manufacture of development systems is iso 9001 certified. note the following details of the code protection feature on microchip devices: ? microchip products meet the specification contained in their particular microchip data sheet. ? microchip believes that its family of products is one of the most secure families of its kind on the market today, when used i n the intended manner and under normal conditions. ? there are dishonest and possibly illegal methods used to breach the code protection feature. all of these methods, to our know l- edge, require using the microchip products in a manner outside the operating specifications contained in microchip's data sheets. most likely, the person doing so is engaged in theft of intellectual property. ? microchip is willing to work with the customer who is concerned about the integrity of their code. ? neither microchip nor any other semiconductor manufacturer can guarantee the security of their code. code protection does not mean that we are guaranteeing the product as ?unbreakable.? code protection is constantly evolving. we at microchip are committed to continuously improving the code protection features of our products.
? 2002 microchip technology inc. preliminary ds30485a-page 1 pic18fxx39 high performance risc cpu: ? linear program memory addressing to 24 kbytes ? linear data memory addressing to 1.4 kbytes ? 20 mhz operation (5 mips): - 20 mhz oscillator/clock input - 5 mhz oscillator/clock input with pll active ? 16-bit wide instructions, 8-bit wide data path ? 8 x 8 single cycle hardware multiplier special microcontroller features: ? 100,000 erase/write cycle enhanced flash program memory typical ? 1,000,000 erase/write cycle data eeprom memory ? flash/data eeprom retention: > 100 years ? power-on reset (por), power-up timer (pwrt) and oscillator start-up timer (ost) ? programmable code protection ? power saving sleep mode ? single supply 5v in-circuit serial programming? (icsp?) via two pins ? in-circuit debug (icd) via two pins analog features: ? compatible 10-bit analog-to-digital converter module (a/d) with: - fast sampling rate - conversion available during sleep - dnl = 1 lsb, inl = 1 lsb ? programmable low voltage detection (plvd) - supports interrupt on low voltage detection ? programmable brown-out reset (bor) peripheral features: ? high current sink/source 25 ma/25 ma ? three external interrupt pins ? timer0 module: 8-bit/16-bit timer/counter with 8-bit programmable prescaler ? timer1 module: 16-bit timer/counter ? timer3 module: 16-bit timer/counter ? secondary oscillator clock option - timer1/timer3 ? two pwm modules: - resolution is 1- to 10-bit, max. pwm freq. @ 8-bit resolution = 156 khz 10-bit resolution = 39 khz ? single phase induction motor control kernel - programmable motor control technology (prompt?) provides open loop variable frequency (vf) control - user programmable voltage vs. frequency curve - most suitable for shaded pole and permanent split capacitor type motors ? master synchronous serial port (mssp) module with two modes of operation: - 3-wire spi? (supports all 4 spi modes) -i 2 c? master and slave mode ? addressable usart module: - supports rs-485 and rs-232 ? parallel slave port (psp) module cmos technology: ? low power, high speed flash/eeprom technology ? fully static design ? wide operating voltage range (2.0v to 5.5v) ? industrial and extended temperature ranges device program memory data memory i/o pins 10-bit a/d (ch) pwm 10-bit mssp ausart timers 16-bit/wdt bytes words sram (bytes) eeprom (bytes) spi master i 2 c pic18f2439 12k 6144 640 256 21 5 2 yes yes yes 3/1 pic18f2539 24k 12288 1408 256 21 5 2 yes yes yes 3/1 pic18f4439 12k 6144 640 256 32 8 2 yes yes yes 3/1 pic18F4539 24k 12288 1408 256 32 8 2 yes yes yes 3/1 enhanced flash microcontrollers with single phase induction motor control kernel
pic18fxx39 ds30485a-page 2 preliminary ? 2002 microchip technology inc. pin diagrams 10 11 2 3 4 5 6 1 18 19 20 21 22 12 13 14 15 38 8 7 44 43 42 41 40 39 16 17 29 30 31 32 33 23 24 25 26 27 28 36 34 35 9 pic18f4439 37 ra3/an3/v ref + ra2/an2/v ref - ra1/an1 ra0/an0 mclr /v pp nc rb7/pgd rb6/pgc rb5/pgm rb4 nc rc6/tx/ck rc5/sdo rc4/sdi/sda rd3/psp3 rd2/psp2 rd1/psp1 rd0/psp0 rc3/sck/scl pwm1 pwm2 nc nc rc0/t13cki osc2/clko/ra6 osc1/clki v ss v dd re2/an7/cs re1/an6/wr re0/an5/rd ra5/an4/ss /lvdin ra4/t0cki rc7/rx/dt rd4/psp4 rd5/psp5 rd6/psp6 rd7/psp7 v ss v dd rb0/int0 rb1/int1 rb2/int2 rb3 44-pin tqfp pic18F4539 10 11 2 3 4 5 6 1 18 19 20 21 22 12 13 14 15 38 8 7 44 43 42 41 40 39 16 17 29 30 31 32 33 23 24 25 26 27 28 36 34 35 9 pic18f4439 37 ra3/an3/v ref + ra2/an2/v ref - ra1/an1 ra0/an0 mclr /v pp rb3 rb7/pgd rb6/pgc rb5/pgm rb4 nc rc6/tx/ck rc5/sdo rc4/sdi/sda rd3/psp3 rd2/psp2 rd1/psp1 rd0/psp0 rc3/sck/scl pwm2 pwm1 rc0/t13cki osc2/clko/ra6 osc1/clki v ss av ss v dd v dd re2/an7/cs re1/an6/wr re0/an5/rd ra5/an4/ss /lvdin ra4/t0cki rc7/rx/dt rd4/psp4 rd5/psp5 rd6/psp6 rd7/psp7 v ss v dd a vdd rb0/int0 rb1/int1 rb2/int2 44-pin qfn pic18F4539
? 2002 microchip technology inc. preliminary ds30485a-page 3 pic18fxx39 pin diagrams (cont.?d) rb7/pgd rb6/pgc rb5/pgm rb4 rb3 rb2/int2 rb1/int1 rb0/int0 v dd v ss rd7/psp7 rd6/psp6 rd5/psp5 rd4/psp4 rc7/rx/dt rc6/tx/ck rc5/sdo rc4/sdi/sda rd3/psp3 rd2/psp2 mclr /v pp ra0/an0 ra1/an1 ra2/an2/v ref - ra3/an3/v ref + ra4/t0cki ra5/an4/ss /lvdin re0/an5/rd re1/an6/wr re2/an7/cs v dd v ss osc1/clki osc2/clko/ra6 rc0/t13cki pwm2 pwm1 rc3/sck/scl rd0/psp0 rd1/psp1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 pic18f4439 pic18f2439 10 11 2 3 4 5 6 1 8 7 9 12 13 14 15 16 17 18 19 20 23 24 25 26 27 28 22 21 mclr /v pp ra0/an0 ra1/an1 ra2/an2/v ref - ra3/an3/v ref + ra4/t0cki ra5/an4/ss /lvdin v ss osc1/clki osc2/clko/ra6 rc0/t13cki pwm2 pwm1 rc3/sck/scl rb7/pgd rb6/pgc rb5/pgm rb4 rb3 rb2/int2 rb1/int1 rb0/int0 v dd v ss rc7/rx/dt rc6/tx/ck rc5/sdo rc4/sdi/sda 40-pin dip 28-pin dip, soic pic18F4539 pic18f2539
pic18fxx39 ds30485a-page 4 preliminary ? 2002 microchip technology inc. table of contents 1.0 device overview ............................................................................................................. ............................................................. 7 2.0 oscillator configurations ................................................................................................... ......................................................... 19 3.0 reset ....................................................................................................................... ................................................................... 23 4.0 memory organization ......................................................................................................... ........................................................ 33 5.0 flash program memory ........................................................................................................ ................................................... 51 6.0 data eeprom memory .......................................................................................................... ................................................... 61 7.0 8 x 8 hardware multiplier ................................................................................................... ........................................................ 67 8.0 interrupts .................................................................................................................. .................................................................. 69 9.0 i/o ports ................................................................................................................... .................................................................. 83 10.0 timer0 module .............................................................................................................. ............................................................. 99 11.0 timer1 module .............................................................................................................. ........................................................... 103 12.0 timer2 module .............................................................................................................. ........................................................... 107 13.0 timer3 module .............................................................................................................. ........................................................... 109 14.0 single phase induction motor control kernel ................................................................................ .......................................... 113 15.0 pulse width modulation (pwm) modules ....................................................................................... .......................................... 123 16.0 master synchronous serial port (mssp) module ............................................................................... ..................................... 125 17.0 addressable universal synchronous asynchronous receiver transmitter (usart)................................................ .............. 165 18.0 compatible 10-bit analog-to-digital converter (a/d) module................................................................. .................................. 181 19.0 low voltage detect ......................................................................................................... ......................................................... 189 20.0 special features of the cpu ................................................................................................ .................................................... 195 21.0 instruction set summary .................................................................................................... ...................................................... 211 22.0 development support........................................................................................................ ....................................................... 253 23.0 electrical characteristics ................................................................................................. ......................................................... 259 24.0 dc and ac characteristics graphs and tables ................................................................................ ....................................... 287 25.0 packaging information...................................................................................................... ........................................................ 297 appendix a: revision history................................................................................................... .......................................................... 305 appendix b: device differences................................................................................................. ........................................................ 305 appendix c: conversion considerations .......................................................................................... ................................................. 306 appendix d: migration from high-end to enhanced devices........................................................................ ..................................... 307 index .......................................................................................................................... ........................................................................ 309 on-line support................................................................................................................ ................................................................. 317 systems information and upgrade hot line ....................................................................................... ............................................... 317 reader response ................................................................................................................ .............................................................. 318 pic18fxx39 product identification system....................................................................................... ................................................ 319
? 2002 microchip technology inc. preliminary ds30485a-page 5 pic18fxx39 to our valued customers it is our intention to provide our valued customers with the best documentation possible to ensure successful use of your micro chip products. to this end, we will continue to improve our publications to better suit your needs. our publications will be refined and enhanced as new volumes and updates are introduced. if you have any questions or comments regarding this publication, please contact the marketing communications department via e-mail at docerrors@mail.microchip.com or fax the reader response form in the back of this data sheet to (480) 792-4150. we welcome your feedback. most current data sheet to obtain the most up-to-date version of this data sheet, please register at our worldwide web site at: http://www.microchip.com you can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page . the last character of the literature number is the version number, (e.g., ds30000a is version a of document ds30000). errata an errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for curren t devices. as device/documentation issues become known to us, we will publish an errata sheet. the errata will specify the revisi on of silicon and revision of document to which it applies. to determine if an errata sheet exists for a particular device, please check with one of the following: ? microchip?s worldwide web site; http://www.microchip.com ? your local microchip sales office (see last page) ? the microchip corporate literature center; u.s. fax: (480) 792-7277 when contacting a sales office or the literature center, please specify which device, revision of silicon and data sheet (inclu de liter- ature number) you are using. customer notification system register on our web site at www.microchip.com/cn to receive the most current information on all of our products.
pic18fxx39 ds30485a-page 6 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 7 pic18fxx39 1.0 device overview this document contains device specific information for the following devices: this family offers the advantages of all pic18 micro- controllers - namely, high computational performance at an economical price - with the addition of high-endur- ance enhanced flash program memory. the pic18fxx39 family also provides an off-the-shelf solu- tion for simple motor control applications, allowing users to create speed control solutions with small part counts and short development times. 1.1 key features 1.1.1 programmable motor processor technology (prompt?) motor control the integrated motor control kernel uses on-chip pulse width modulation (pwm) to provide speed control for single phase induction motors. through a convenient set of application program interfaces (apis) and vari- able frequency technology for open loop control, users can develop applications with little or no previous expe- rience in motor control techniques. prompt motor con- trol provides modulated output over a range of 0 to 127 hz, and has a pre-defined v/f curve that can be reprogrammed to suit the application. 1.1.2 other pic18fxx39 features ? memory endurance: the enhanced flash cells for both program memory and data eeprom are rated to last for many thousands of erase/write cycles - up to 100,000 for program memory, and 1,000,000 for eeprom. data retention without refresh is conservatively estimated to be greater than 100 years at 25c. ? self-programmability: these devices can write to their own program memory spaces under internal software control. by using a bootloader routine located in the protected boot block at the top of pro- gram memory, it becomes possible to create an application that can update itself in the field. ? addressable usart: this serial communication module is capable of standard rs-232 operation using the internal oscillator block, removing the need for an external crystal (and its accompanying power requirement) in applications that talk to the outside world. ? 10-bit a/d converter: this module offers up to 8 conversion channels for flexibility in sensor monitoring and control, as well as the ability to do conversions while the device is in sleep mode. 1.2 details on individual family members devices in the pic18fxx39 family are available in 28-pin (pic18f2x39) and 40/44-pin (pic18f4x39) packages. block diagrams for the two groups are shown in figure 1-1 and figure 1-2. the devices are differentiated from each other in four ways: 1. flash program memory and data ram (12 kbytes and 640 bytes for pic18fx439 devices, 24 kbytes and 1408 bytes for pic18fx539) 2. a/d channels (5 for pic18f2x39 devices, 8 for pic18f4x39) 3. i/o ports (3 ports on pic18f2x39, 5 ports on pic18f4x39 devices) 4. parallel slave port (present only on pic18f4x39 devices) all other features for devices in this family are identical. these are summarized in table 1-1. the pinouts for all devices are listed in table 1-2 and table 1-3. ? pic18f2439 ? pic18f4439 ? pic18f2539 ? pic18F4539
pic18fxx39 ds30485a-page 8 preliminary ? 2002 microchip technology inc. table 1-1: pic18fxx39 device features features pic18f2439 pic18f2539 pic18f4439 pic18F4539 operating frequency dc - 40 mhz dc - 40 mhz dc - 40 mhz dc - 40 mhz program memory (bytes) 12k 24k 12k 24k program memory (instructions) 6144 12288 6144 12288 data memory (bytes) 640 1408 640 1408 data eeprom memory (bytes) 256 256 256 256 interrupt sources 15 15 16 16 i/o ports ports a, b, c ports a, b, c ports a, b, c, d, e ports a, b, c, d, e timers 3 3 3 3 pwm modules (1) 2222 single phase induction motor control yes yes yes yes serial communications mssp, addressable usart mssp, addressable usart mssp, addressable usart mssp, addressable usart parallel communications ? ? psp psp 10-bit analog-to-digital module 5 input channels 5 input channels 8 input channels 8 input channels resets (and delays) por, bor, reset instruction, stack full, stack underflow (pwrt, ost) por, bor, reset instruction, stack full, stack underflow (pwrt, ost) por, bor, reset instruction, stack full, stack underflow (pwrt, ost) por, bor, reset instruction, stack full, stack underflow (pwrt, ost) programmable low voltage detect yes yes yes yes programmable brown-out reset yes yes yes yes instruction set 75 instructions 75 instructions 75 instructions 75 instructions packages 28-pin dip 28-pin soic 28-pin dip 28-pin soic 40-pin dip 44-pin tqfp 44-pin qfn 40-pin dip 44-pin tqfp 44-pin qfn note 1: pwm modules are used exclusively in conjunction with the motor control kernel, and are not available for other applications.
? 2002 microchip technology inc. preliminary ds30485a-page 9 pic18fxx39 figure 1-1: pic18f2x39 block diagram instruction decode & control porta portb portc ra4/t0cki ra5/an4/ss /lvdin rc0/t13cki rc3/sck/scl rc4/sdi/sda rc5/sdo rc6/tx/ck rc7/rx/dt note 1: the high order bits of the direct address for the ram are from the bsr register (except for the movff instruction). 2: many of the general purpose i/o pins are multiplexed with one or more peripheral module functions. the multiplexing combination s are device dependent. addressable pwm1 synchronous timer0 timer1 timer2 serial port ra3/an3/v ref + ra2/an2/v ref - ra1/an1 ra0/an0 a/d converter data latch data ram address latch address<12> 12 ( 2 ) bsr fsr0 fsr1 fsr2 4 12 4 pch pcl pclath 8 31 level stack program counter prodl prodh 8 x 8 multiply wreg 8 bit op 8 8 alu<8> 8 address latch program memory (up to 2 mbytes) data latch 21 21 16 8 8 8 inc/dec logic 21 8 data bus<8> 8 instruction 12 3 rom latch timer3 pwm2 bank0, f pclatu pcu ra6 usart master 8 register table latch table pointer inc/dec logic decode rb0/int0 rb4 rb1/int1 rb2/int2 rb3 rb5/pgm rb6/pgc rb7/pgd data eeprom power-up timer oscillator start-up timer power-on reset watchdog timer osc1/clki osc2/clko mclr v dd , v ss brown-out reset timing generation 4x pll t1osci t1osco precision reference voltage low voltage programming in-circuit debugger pwm1 pwm2
pic18fxx39 ds30485a-page 10 preliminary ? 2002 microchip technology inc. figure 1-2: pic18f4x39 block diagram power-up timer oscillator start-up timer power-on reset watchdog timer instruction decode & control osc1/clki osc2/clko mclr v dd , v ss porta portb portc ra4/t0cki ra5/an4/ss /lvdin rb0/int0 rb4 rc0/t13cki rc3/sck/scl rc4/sdi/sda rc5/sdo rc6/tx/ck rc7/rx/dt brown-out reset note 1: the high order bits of the direct address for the ram are from the bsr register (except for the movff instruction). 2: many of the general purpose i/o pins are multiplexed with one or more peripheral module functions. the multiplexing combination s are device dependent. addressable pwm1 master timer0 timer1 timer2 serial port ra3/an3/v ref + ra2/an2/v ref - ra1/an1 ra0/an0 parallel slave port timing generation 4x pll a/d converter rb1/int1 data latch data ram (up to 4k address reach) address latch address<12> 12 ( 2 ) bank0, f bsr fsr0 fsr1 fsr2 4 12 4 pch pcl pclath 8 31 level stack program counter prodl prodh 8 x 8 multiply wreg 8 bit op 8 8 alu<8> 8 address latch program memory (up to 2 mbytes) data latch 21 21 16 8 8 8 inc/dec logic 21 8 data bus<8> table latch 8 instruction 12 3 rom latch timer3 portd porte re0/an5/rd re1/an6/wr re2/an7/cs pwm2 rb2/int2 rb3 t1osci t1osco pclatu pcu ra6 precision reference voltage synchronous usart register 8 table pointer inc/dec logic decode rd0/psp0 rd1/psp1 rd2/psp2 rd3/psp3 rd4/psp4 rd5/psp5 rd6/psp6 rd7/psp7 low voltage programming in-circuit debugger data eeprom rb5/pgm rb6/pgc rb7/pgd pwm1 pwm2
? 2002 microchip technology inc. preliminary ds30485a-page 11 pic18fxx39 table 1-2: pic18f2x39 pinout i/o descriptions pin name pin number pin type buffer type description dip soic mclr /v pp mclr v pp 11 i i st st master clear (input) or high voltage icsp programming enable pin. master clear (reset) input. this pin is an active low reset to the device. high voltage icsp programming enable pin. nc ? ? ? ? these pins should be left unconnected. osc1/clki osc1 clki 99 i i cmos cmos oscillator crystal or external clock input. oscillator crystal input or external clock source input. external clock source input. always associated with pin function osc1. (see related osc1/clki, osc2/clko pins.) osc2/clko/ra6 osc2 clko ra6 10 10 o o i/o ? ? ttl oscillator crystal or clock output. oscillator crystal output. connects to crystal or resonator in crystal oscillator mode. in ec mode, osc2 pin outputs clko which has 1/4 the frequency of osc1, and denotes the instruction cycle rate. general purpose i/o pin. porta is a bi-directional i/o port. ra0/an0 ra0 an0 22 i/o i ttl analog digital i/o. analog input 0. ra1/an1 ra1 an1 33 i/o i ttl analog digital i/o. analog input 1. ra2/an2/v ref - ra2 an2 v ref - 44 i/o i i ttl analog analog digital i/o. analog input 2. a/d reference voltage (low) input. ra3/an3/v ref + ra3 an3 v ref + 55 i/o i i ttl analog analog digital i/o. analog input 3. a/d reference voltage (high) input. ra4/t0cki ra4 t0cki 66 i/o i st/od st digital i/o. open drain when configured as output. timer0 external clock input. ra5/an4/ss /lvdin ra5 an4 ss lvdin 77 i/o i i i ttl analog st analog digital i/o. analog input 4. spi slave select input. low voltage detect input. ra6 see the osc2/clko/ra6 pin. legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open drain (no p diode to v dd )
pic18fxx39 ds30485a-page 12 preliminary ? 2002 microchip technology inc. portb is a bi-directional i/o port. portb can be software programmed for internal weak pull-ups on all inputs. rb0/int0 rb0 int0 21 21 i/o i ttl st digital i/o. external interrupt 0. rb1/int1 rb1 int1 22 22 i/o i ttl st digital i/o. external interrupt 1. rb2/int2 rb2 int2 23 23 i/o i ttl st digital i/o. external interrupt 2. rb3 24 24 i/o ttl digital i/o. rb4 25 25 i/o ttl digital i/o. interrupt-on-change pin. rb5/pgm rb5 pgm 26 26 i/o i/o ttl st digital i/o. interrupt-on-change pin. low voltage icsp programming enable pin. rb6/pgc rb6 pgc 27 27 i/o i/o ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming clock pin. rb7/pgd rb7 pgd 28 28 i/o i/o ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming data pin. table 1-2: pic18f2x39 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip soic legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open drain (no p diode to v dd )
? 2002 microchip technology inc. preliminary ds30485a-page 13 pic18fxx39 portc is a bi-directional i/o port. rc0/t13cki rc0 t13cki 11 11 i/o i st st digital i/o. timer1/timer3 external clock input. rc3/sck/scl rc3 sck scl 14 14 i/o i/o i/o st st st digital i/o. synchronous serial clock input/output for spi mode. synchronous serial clock input/output for i 2 c mode. rc4/sdi/sda rc4 sdi sda 15 15 i/o i i/o st st st digital i/o. spi data in. i 2 c data i/o. rc5/sdo rc5 sdo 16 16 i/o o st ? digital i/o. spi data out. rc6/tx/ck rc6 tx ck 17 17 i/o o i/o st ? st digital i/o. usart asynchronous transmit. usart synchronous clock (see related rx/dt). rc7/rx/dt rc7 rx dt 18 18 i/o i i/o st st st digital i/o. usart asynchronous receive. usart synchronous data (see related tx/ck). pwm1 13 13 o ? pwm channel 1 (motor control) output. pwm2 12 12 o ? pwm channel 2 (motor control) output. v ss 8, 19 8, 19 p ? ground reference for logic and i/o pins. v dd 20 20 p ? positive supply for logic and i/o pins. table 1-2: pic18f2x39 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip soic legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open drain (no p diode to v dd )
pic18fxx39 ds30485a-page 14 preliminary ? 2002 microchip technology inc. table 1-3: pic18f4x39 pinout i/o descriptions pin name pin number pin type buffer type description dip qfn tqfp mclr /v pp mclr v pp 11818 i i st st master clear (input) or high voltage icsp programming enable pin. master clear (reset) input. this pin is an active low reset to the device. high voltage icsp programming enable pin. osc1/clki osc1 clki 13 32 30 i i cmos cmos oscillator crystal or external clock input. oscillator crystal input or external clock source input. external clock source input. always associated with pin function osc1. (see related osc1/clki, osc2/clko pins.) osc2/clko/ra6 osc2 clko ra6 14 33 31 o o i/o ? ? ttl oscillator crystal or clock output. oscillator crystal output. connects to crystal or resonator in crystal oscillator mode. in ec mode, osc2 pin outputs clko, which has 1/4 the frequency of osc1 and denotes the instruction cycle rate. general purpose i/o pin. porta is a bi-directional i/o port. ra0/an0 ra0 an0 21919 i/o i ttl analog digital i/o. analog input 0. ra1/an1 ra1 an1 32020 i/o i ttl analog digital i/o. analog input 1. ra2/an2/v ref - ra2 an2 v ref - 42121 i/o i i ttl analog analog digital i/o. analog input 2. a/d reference voltage (low) input. ra3/an3/v ref + ra3 an3 v ref + 52222 i/o i i ttl analog analog digital i/o. analog input 3. a/d reference voltage (high) input. ra4/t0cki ra4 t0cki 62323 i/o i st/od st digital i/o. open drain when configured as output. timer0 external clock input. ra5/an4/ss /lvdin ra5 an4 ss lvdin 72424 i/o i i i ttl analog st analog digital i/o. analog input 4. spi slave select input. low voltage detect input. ra6 (see the osc2/clko/ra6 pin.) legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o=output p =power od = open drain (no p diode to v dd )
? 2002 microchip technology inc. preliminary ds30485a-page 15 pic18fxx39 portb is a bi-directional i/o port. portb can be software programmed for internal weak pull-ups on all inputs. rb0/int0 rb0 int0 33 9 8 i/o i ttl st digital i/o. external interrupt 0. rb1/int1 rb1 int1 34 10 9 i/o i ttl st digital i/o. external interrupt 1. rb2/int2 rb2 int2 35 11 10 i/o i ttl st digital i/o. external interrupt 2. rb3 36 12 11 i/o ttl digital i/o. rb4 37 14 14 i/o ttl digital i/o. interrupt-on-change pin. rb5/pgm rb5 pgm 38 15 15 i/o i/o ttl st digital i/o. interrupt-on-change pin. low voltage icsp programming enable pin. rb6/pgc rb6 pgc 39 16 16 i/o i/o ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming clock pin. rb7/pgd rb7 pgd 40 17 17 i/o i/o ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming data pin. table 1-3: pic18f4x39 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip qfn tqfp legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o=output p =power od = open drain (no p diode to v dd )
pic18fxx39 ds30485a-page 16 preliminary ? 2002 microchip technology inc. portc is a bi-directional i/o port. rc0/t13cki rc0 t13cki 15 34 32 i/o i st st digital i/o. timer1/timer3 external clock input. rc3/sck/scl rc3 sck scl 18 37 37 i/o i/o i/o st st st digital i/o. synchronous serial clock input/output for spi mode. synchronous serial clock input/output for i 2 c mode. rc4/sdi/sda rc4 sdi sda 23 42 42 i/o i i/o st st st digital i/o. spi data in. i 2 c data i/o. rc5/sdo rc5 sdo 24 43 43 i/o o st ? digital i/o. spi data out. rc6/tx/ck rc6 tx ck 25 44 44 i/o o i/o st ? st digital i/o. usart asynchronous transmit. usart synchronous clock (see related rx/dt). rc7/rx/dt rc7 rx dt 26 1 1 i/o i i/o st st st digital i/o. usart asynchronous receive. usart synchronous data (see related tx/ck). pwm1 17 35 36 o ? pwm channel 1 (motor control) output. pwm2 16 36 35 o ? pwm channel 2 (motor control) output. table 1-3: pic18f4x39 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip qfn tqfp legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o=output p =power od = open drain (no p diode to v dd )
? 2002 microchip technology inc. preliminary ds30485a-page 17 pic18fxx39 portd is a bi-directional i/o port, or a parallel slave port (psp) for interfacing to a microprocessor port. these pins have ttl input buffers when psp module is enabled. rd0/psp0 rd0 psp0 19 38 38 i/o st ttl digital i/o. parallel slave port data. rd1/psp1 rd1 psp1 20 39 39 i/o st ttl digital i/o. parallel slave port data. rd2/psp2 rd2 psp2 21 40 40 i/o st ttl digital i/o. parallel slave port data. rd3/psp3 rd3 psp3 22 41 41 i/o st ttl digital i/o. parallel slave port data. rd4/psp4 rd4 psp4 27 2 2 i/o st ttl digital i/o. parallel slave port data. rd5/psp5 rd5 psp5 28 3 3 i/o st ttl digital i/o. parallel slave port data. rd6/psp6 rd6 psp6 29 4 4 i/o st ttl digital i/o. parallel slave port data. rd7/psp7 rd7 psp7 30 5 5 i/o st ttl digital i/o. parallel slave port data. table 1-3: pic18f4x39 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip qfn tqfp legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o=output p =power od = open drain (no p diode to v dd )
pic18fxx39 ds30485a-page 18 preliminary ? 2002 microchip technology inc. porte is a bi-directional i/o port. re0/rd /an5 re0 rd an5 8 25 25 i/o st ttl analog digital i/o. read control for parallel slave port (see also wr and cs pins). analog input 5. re1/wr /an6 re1 wr an6 9 26 26 i/o st ttl analog digital i/o. write control for parallel slave port (see cs and rd pins). analog input 6. re2/cs /an7 re2 cs an7 10 27 27 i/o st ttl analog digital i/o. chip select control for parallel slave port (see related rd and wr ). analog input 7. v ss 12, 31 6, 31 6, 29 p ? ground reference for logic and i/o pins. v dd 11, 32 7, 28, 29 7, 28 p ? positive supply for logic and i/o pins. av ss ? 30 ? p ? ground reference for analog modules. av dd ? 8 ? p ? positive supply for analog modules. nc ? 13 12, 13, 33, 34 ? ? these pins should be left unconnected. table 1-3: pic18f4x39 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip qfn tqfp legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o=output p =power od = open drain (no p diode to v dd )
? 2002 microchip technology inc. preliminary ds30485a-page 19 pic18fxx39 2.0 oscillator configurations 2.1 oscillator types the pic18fxx39 can be operated in four different oscillator modes at a frequency of 20 mhz. the user can program three configuration bits (fosc2, fosc1, and fosc0) to select one of these four modes: 1. hs high speed crystal/resonator 2. hs + pll high speed crystal/resonator with pll enabled using 5 mhz crystal 3. ec external clock 4. ecio external clock with i/o pin enabled 2.2 crystal oscillator/ceramic resonators in hs or hs+pll oscillator modes, a crystal or ceramic resonator is connected to the osc1 and osc2 pins to establish oscillation. figure 2-1 shows the pin connections. the pic18fxx39 oscillator design requires the use of a parallel cut crystal. figure 2-1: crystal/ceramic resonator operation (hs configuration) an external clock source may also be connected to the osc1 pin in the hs mode, as shown in figure 2-2. figure 2-2: external clock input operation (hs osc configuration) note: the operation of the motor control kernel and its apis (section 14.0) is based on an assumed clock frequency of 20 mhz. changing the oscillator frequency will change the timing used in the motor con- trol kernel accordingly. to achieve the best results in motor control applications, a clock frequency of 20 mhz is highly recommended. note: use of a series cut crystal may give a fre- quency out of the crystal manufacturers specifications. note 1: higher capacitance increases the stability of the oscillator, but also increases the start-up time. 2: since each resonator/crystal has its own characteristics, the user should consult the resonator/crystal manufacturer for appro- priate values of external components, or verify oscillator performance. note 1: see table 2-1 for recommended values of c1 and c2. 2: a series resistor (r s ) may be required for at strip cut crystals. 3: r f varies with the oscillator mode chosen. c1 (1) c2 (1) xtal osc2 osc1 r f (3) sleep to logic pic18fxx39 r s (2) internal osc1 osc2 open clock from ext. system pic18fxx39
pic18fxx39 ds30485a-page 20 preliminary ? 2002 microchip technology inc. table 2-1: capacitor selection for crystal oscillator 2.3 external clock input the ec and ecio oscillator modes require an external clock source to be connected to the osc1 pin. the feedback device between osc1 and osc2 is turned off in these modes to save current. there is no oscilla- tor start-up time required after a power-on reset or after a recovery from sleep mode. in the ec oscillator mode, the oscillator frequency divided by 4 is available on the osc2 pin. this signal may be used for test purposes or to synchronize other logic. figure 2-3 shows the pin connections for the ec oscillator mode. figure 2-3: external clock input operation (ec configuration) the ecio oscillator mode functions like the ec mode, except that the osc2 pin becomes an additional gen- eral purpose i/o pin. the i/o pin becomes bit 6 of porta (ra6). figure 2-4 shows the pin connections for the ecio oscillator mode. figure 2-4: external clock input operation (ecio configuration) 2.4 hs/pll a phase locked loop circuit is provided as a program- mable option for users that want to multiply the fre- quency of the incoming crystal oscillator signal by 4. for an input clock frequency of 5 mhz, the internal clock frequency will be multiplied to 20 mhz. this is useful for customers who are concerned with emi due to high frequency crystals. the pll can only be enabled when the oscillator con- figuration bits are programmed for hs mode. if they are programmed for any other mode, the pll is not enabled and the system clock will come directly from osc1. the pll is one of the modes specified by the fosc<2:0> configuration bits. the oscillator mode is specified during device programming. a pll lock timer is used to ensure that the pll has locked before device execution starts. the pll lock timer has a time-out that is called t pll . ranges tested: mode freq c1 c2 hs 20.0 mhz 15-33 pf 15-33 pf these values are for design guidance only. see notes following this table. crystals used 20.0 mhz epson ca-301 20.000m-c 30 ppm note 1: higher capacitance increases the stability of the oscillator, but also increases the start-up time. 2: rs may be required in hs mode to avoid overdriving crystals with low drive level specification. 3: since each resonator/crystal has its own characteristics, the user should consult the resonator/crystal manufacturer for appro- priate values of external components, or verify oscillator performance. osc1 osc2 f osc /4 clock from ext. system pic18fxx39 osc1 i/o (osc2) ra6 clock from ext. system pic18fxx39
? 2002 microchip technology inc. preliminary ds30485a-page 21 pic18fxx39 figure 2-5: pll block diagram 2.5 effects of sleep mode on the on-chip oscillator when the device executes a sleep instruction, the oscillator is turned off and the device is held at the beginning of an instruction cycle (q1 state). with the oscillator off, the osc1 and osc2 signals will stop oscillating. since all the transistor switching currents have been removed, sleep mode achieves the lowest current consumption of the device (only leakage cur- rents). enabling any on-chip feature that will operate during sleep will increase the current consumed dur- ing sleep. the user can wake from sleep through external reset, watchdog timer reset, or through an interrupt. 2.6 power-up delays power-up delays are controlled by two timers, so that no external reset circuitry is required for most appli- cations. the delays ensure that the device is kept in reset, until the device power supply and clock are stable. for additional information on reset operation, see section 3.0. the first timer is the power-up timer (pwrt), which optionally provides a fixed delay of 72 ms (nominal) on power-up only (por and bor). the second timer is the oscillator start-up timer (ost), intended to keep the chip in reset until the crystal oscillator is stable. with the pll enabled (hs/pll oscillator mode), the time-out sequence following a power-on reset is differ- ent from other oscillator modes. the time-out sequence is as follows: 1. the pwrt time-out is invoked after a por time delay has expired. 2. the oscillator start-up timer (ost) is invoked. however, this is still not a sufficient amount of time to allow the pll to lock at high frequencies. 3. the pwrt timer is used to provide an additional fixed 2 ms (nominal) time-out to allow the pll ample time to lock to the incoming clock frequency. table 2-2: osc1 and osc2 pin states in sleep mode mux vco loop filter 4 crystal osc osc2 osc1 pll enable f in f out sysclk phase comparator (from configuration hs osc bit register) osc mode osc1 pin osc2 pin ecio floating configured as porta, bit 6 ec floating at logic low hs feedback inverter disabled, at quiescent voltage level feedback inverter disabled, at quiescent voltage level note: see table 3-1 in the ?reset? section, for time-outs due to sleep and mclr reset.
pic18fxx39 ds30485a-page 22 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 23 pic18fxx39 3.0 reset the pic18fxx39 differentiates between various kinds of reset: a) power-on reset (por) b) mclr reset during normal operation c) mclr reset during sleep d) watchdog timer (wdt) reset (during normal operation) e) programmable brown-out reset (bor) f) reset instruction g) stack full reset h) stack underflow reset most registers are unaffected by a reset. their status is unknown on por and unchanged by all other resets. the other registers are forced to a ?reset state? on power-on reset, mclr , wdt reset, brown- out reset, mclr reset during sleep and by the reset instruction. most registers are not affected by a wdt wake-up, since this is viewed as the resumption of normal oper- ation. status bits from the rcon register, ri , to , pd , por and bor, are set or cleared differently in different reset situations, as indicated in table 3-2. these bits are used in software to determine the nature of the reset. see table 3-3 for a full description of the reset states of all registers. a simplified block diagram of the on-chip reset circuit is shown in figure 3-1. the enhanced mcu devices have a mclr noise filter in the mclr reset path. the filter will detect and ignore small pulses. the mclr pin is not driven low by any internal resets, including the wdt. figure 3-1: simplified block diagram of on-chip reset circuit s r q external reset mclr v dd osc1 wdt module v dd rise detect ost/pwrt on-chip rc osc (1) wdt time-out power-on reset ost 10-bit ripple counter pwrt chip_reset 10-bit ripple counter reset enable ost (2) enable pwrt sleep note 1: this is a separate oscillator from the rc oscillator of the clki pin. 2: see table 3-1 for time-out situations. brown-out reset boren reset instruction stack pointer stack full/underflow reset
pic18fxx39 ds30485a-page 24 preliminary ? 2002 microchip technology inc. 3.1 power-on reset (por) a power-on reset pulse is generated on-chip when v dd rise is detected. to take advantage of the por cir- cuitry, just tie the mclr pin directly (or through a resis- tor) to v dd . this will eliminate external rc components usually needed to create a power-on reset delay. a minimum rise rate for v dd is specified (parameter d004). for a slow rise time, see figure 3-2. when the device starts normal operation (i.e., exits the reset condition), device operating parameters (volt- age, frequency, temperature, etc.) must be met to ensure operation. if these conditions are not met, the device must be held in reset until the operating conditions are met. figure 3-2: external power-on reset circuit (for slow v dd power-up) 3.2 power-up timer (pwrt) the power-up timer provides a fixed nominal time-out (parameter 33) only on power-up from the por. the power-up timer operates on an internal rc oscillator. the chip is kept in reset as long as the pwrt is active. the pwrt?s time delay allows v dd to rise to an acceptable level. a configuration bit is provided to enable/disable the pwrt. the power-up time delay will vary from chip-to-chip due to v dd , temperature and process variation. see dc parameter d033 for details. 3.3 oscillator start-up timer (ost) the oscillator start-up timer (ost) provides a 1024 oscillator cycle (from osc1 input) delay after the pwrt delay is over (parameter 32). this ensures that the crystal oscillator or resonator has started and stabilized. the ost time-out is invoked only for xt, lp and hs modes and only on power-on reset or wake-up from sleep. 3.4 pll lock time-out with the pll enabled, the time-out sequence following a power-on reset is different from other oscillator modes. a portion of the power-up timer is used to pro- vide a fixed time-out that is sufficient for the pll to lock to the main oscillator frequency. this pll lock time-out (t pll ) is typically 2 ms and follows the oscillator start-up time-out (ost). 3.5 brown-out reset (bor) a configuration bit, boren, can disable (if clear/ programmed), or enable (if set) the brown-out reset circuitry. if v dd falls below parameter d005 for greater than parameter 35, the brown-out situation will reset the chip. a reset may not occur if v dd falls below parameter d005 for less than parameter 35. the chip will remain in brown-out reset until v dd rises above bv dd . if the power-up timer is enabled, it will be invoked after v dd rises above bv dd ; it then will keep the chip in reset for an additional time delay (parameter 33). if v dd drops below bv dd while the power-up timer is running, the chip will go back into a brown-out reset and the power-up timer will be initial- ized. once v dd rises above bv dd , the power-up timer will execute the additional time delay. 3.6 time-out sequence on power-up, the time-out sequence is as follows: first, pwrt time-out is invoked after the por time delay has expired. then, ost is activated. the total time-out will vary based on oscillator configuration and the status of the pwrt. for example, in rc mode with the pwrt disabled, there will be no time-out at all. figure 3-3, figure 3-4, figure 3-5, figure 3-6 and figure 3-7 depict time-out sequences on power-up. since the time-outs occur from the por pulse, if mclr is kept low long enough, the time-outs will expire. bringing mclr high will begin execution immediately (figure 3-5). this is useful for testing purposes or to synchronize more than one pic18fxxx device operating in parallel. table 3-2 shows the reset conditions for some special function registers, while table 3-3 shows the reset conditions for all the registers. note 1: external power-on reset circuit is required only if the v dd power-up slope is too slow. the diode d helps discharge the capacitor quickly when v dd powers down. 2: r < 40 k ? is recommended to make sure that the voltage drop across r does not violate the device?s electrical specification. 3: r1 = 100 ? to 1 k ? will limit any current flow- ing into mclr from external capacitor c, in the event of mclr/ v pp pin breakdown due to electrostatic discharge (esd) or electrical overstress (eos). c r1 r d v dd mclr pic18fxxxx
? 2002 microchip technology inc. preliminary ds30485a-page 25 pic18fxx39 table 3-1: time-out in various situations register 3-1: rcon register bits and positions table 3-2: status bits, their significance and the initialization condition for rcon register oscillator configuration power-up (2) brown-out wake-up from sleep or oscillator switch pwrte = 0 pwrte = 1 hs with pll enabled (1) 72 ms + 1024 t osc + 2ms 1024 t osc + 2 ms 72 ms (2) + 1024 t osc + 2 ms 1024 t osc + 2 ms hs, xt, lp 72 ms + 1024 t osc 1024 t osc 72 ms (2) + 1024 t osc 1024 t osc ec 72 ms ? 72 ms (2) ? external rc 72 ms ? 72 ms (2) ? note 1: 2 ms is the nominal time required for the 4x pll to lock. 2: 72 ms is the nominal power-up timer delay, if implemented. r/w-0 u-0 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen ? ?ri to pd por bor bit 7 bit 0 note 1: refer to section 4.14 (page 50) for bit definitions. condition program counter rcon register ri to pd por bor stkful stkunf power-on reset 0000h 0--1 1100 1 1 1 0 0 u u mclr reset during normal operation 0000h 0--u uuuu u u u u u u u software reset during normal operation 0000h 0--0 uuuu 0 u u u u u u stack full reset during normal operation 0000h 0--u uu11 u u u u u u 1 stack underflow reset during normal operation 0000h 0--u uu11 u u u u u 1 u mclr reset during sleep 0000h 0--u 10uu u 1 0 u u u u wdt reset 0000h 0--u 01uu 1 0 1 u u u u wdt wake-up pc + 2 u--u 00uu u 0 0 u u u u brown-out reset 0000h 0--1 11u0 1 1 1 1 0 u u interrupt wake-up from sleep pc + 2 (1) u--u 00uu u 1 0 u u u u legend: u = unchanged, x = unknown, - = unimplemented bit, read as '0' note 1: when the wake-up is due to an interrupt and the gieh or giel bits are set, the pc is loaded with the interrupt vector (0x000008h or 0x000018h).
pic18fxx39 ds30485a-page 26 preliminary ? 2002 microchip technology inc. table 3-3: initialization conditions for all registers register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt tosu 2439 4439 2539 4539 ---0 0000 ---0 0000 ---0 uuuu (1) tosh 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu (1) tosl 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu (1) stkptr 2439 4439 2539 4539 00-0 0000 uu-0 0000 uu-u uuuu (1) pclatu 2439 4439 2539 4539 ---0 0000 ---0 0000 ---u uuuu pclath 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu pcl 2439 4439 2539 4539 0000 0000 0000 0000 pc + 2 (2) tblptru 2439 4439 2539 4539 --00 0000 --00 0000 --uu uuuu tblptrh 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu tblptrl 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu tablat 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu prodh 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu prodl 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu intcon 2439 4439 2539 4539 0000 000x 0000 000u uuuu uuuu (3) intcon2 2439 4439 2539 4539 1111 -1-1 1111 -1-1 uuuu -u-u (3) intcon3 2439 4439 2539 4539 11-0 0-00 11-0 0-00 uu-u u-uu (3) indf0 2439 4439 2539 4539 n/a n/a n/a postinc0 2439 4439 2539 4539 n/a n/a n/a postdec0 2439 4439 2539 4539 n/a n/a n/a preinc0 2439 4439 2539 4539 n/a n/a n/a plusw0 2439 4439 2539 4539 n/a n/a n/a fsr0h 2439 4439 2539 4539 ---- xxxx ---- uuuu ---- uuuu fsr0l 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu wreg 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu indf1 2439 4439 2539 4539 n/a n/a n/a postinc1 2439 4439 2539 4539 n/a n/a n/a postdec1 2439 4439 2539 4539 n/a n/a n/a preinc1 2439 4439 2539 4539 n/a n/a n/a plusw1 2439 4439 2539 4539 n/a n/a n/a legend: u = unchanged, x = unknown, - = unimplemented bit, read as '0', q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. * these registers are retained to maintain compatibility with pic18fxx2 devices; however, one or more bits are reserved. users should not modify the value of these bits. see section 4.9.2 for details. note 1: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 4: see table 3-2 for reset value for specific condition. 5: bit 6 of porta, lata, and trisa are enabled in ecio and rcio oscillator modes only. in all other oscillator modes, they are disabled and read ?0?. 6: bit 6 of porta, lata and trisa are not available on all devices. when unimplemented, they are read ?0?.
? 2002 microchip technology inc. preliminary ds30485a-page 27 pic18fxx39 fsr1h 2439 4439 2539 4539 ---- xxxx ---- uuuu ---- uuuu fsr1l 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu bsr 2439 4439 2539 4539 ---- 0000 ---- 0000 ---- uuuu indf2 2439 4439 2539 4539 n/a n/a n/a postinc2 2439 4439 2539 4539 n/a n/a n/a postdec2 2439 4439 2539 4539 n/a n/a n/a preinc2 2439 4439 2539 4539 n/a n/a n/a plusw2 2439 4439 2539 4539 n/a n/a n/a fsr2h 2439 4439 2539 4539 ---- xxxx ---- uuuu ---- uuuu fsr2l 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu status 2439 4439 2539 4539 ---x xxxx ---u uuuu ---u uuuu tmr0h 2439 4439 2539 4539 0000 0000 uuuu uuuu uuuu uuuu tmr0l 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu t0con 2439 4439 2539 4539 1111 1111 1111 1111 uuuu uuuu osccon * 2439 4439 2539 4539 ---- ---0 ---- ---0 ---- ---u lvdcon 2439 4439 2539 4539 --00 0101 --00 0101 --uu uuuu wdtcon 2439 4439 2539 4539 ---- ---0 ---- ---0 ---- ---u rcon (4) 2439 4439 2539 4539 0--q 11qq 0--q qquu u--u qquu tmr1h 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu tmr1l 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu t1con 2439 4439 2539 4539 0-00 0000 u-uu uuuu u-uu uuuu tmr2 * 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu pr2 * 2439 4439 2539 4539 1111 1111 1111 1111 1111 1111 t2con * 2439 4439 2539 4539 -000 0000 -000 0000 -uuu uuuu sspbuf 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu sspadd 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu sspstat 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu sspcon1 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu sspcon2 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu table 3-3: initialization conditions for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as '0', q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. * these registers are retained to maintain compatibility with pic18fxx2 devices; however, one or more bits are reserved. users should not modify the value of these bits. see section 4.9.2 for details. note 1: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 4: see table 3-2 for reset value for specific condition. 5: bit 6 of porta, lata, and trisa are enabled in ecio and rcio oscillator modes only. in all other oscillator modes, they are disabled and read ?0?. 6: bit 6 of porta, lata and trisa are not available on all devices. when unimplemented, they are read ?0?.
pic18fxx39 ds30485a-page 28 preliminary ? 2002 microchip technology inc. adresh 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu adresl 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu adcon0 2439 4439 2539 4539 0000 00-0 0000 00-0 uuuu uu-u adcon1 2439 4439 2539 4539 00-- 0000 00-- 0000 uu-- uuuu ccpr1h 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu ccpr1l * 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu ccp1con * 2439 4439 2539 4539 --00 0000 --00 0000 --uu uuuu ccpr2h 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu ccpr2l * 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu ccp2con * 2439 4439 2539 4539 --00 0000 --00 0000 --uu uuuu tmr3h 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu tmr3l 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu t3con 2439 4439 2539 4539 0000 0000 uuuu uuuu uuuu uuuu spbrg 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu rcreg 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu txreg 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu txsta 2439 4439 2539 4539 0000 -010 0000 -010 uuuu -uuu rcsta 2439 4439 2539 4539 0000 000x 0000 000x uuuu uuuu eeadr 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu eedata 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu eecon1 2439 4439 2539 4539 xx-0 x000 uu-0 u000 uu-0 u000 eecon2 2439 4439 2539 4539 ---- ---- ---- ---- ---- ---- table 3-3: initialization conditions for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as '0', q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. * these registers are retained to maintain compatibility with pic18fxx2 devices; however, one or more bits are reserved. users should not modify the value of these bits. see section 4.9.2 for details. note 1: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 4: see table 3-2 for reset value for specific condition. 5: bit 6 of porta, lata, and trisa are enabled in ecio and rcio oscillator modes only. in all other oscillator modes, they are disabled and read ?0?. 6: bit 6 of porta, lata and trisa are not available on all devices. when unimplemented, they are read ?0?.
? 2002 microchip technology inc. preliminary ds30485a-page 29 pic18fxx39 ipr2 2439 4439 2539 4539 ---1 1111 ---1 1111 ---u uuuu pir2 2439 4439 2539 4539 ---0 0000 ---0 0000 ---u uuuu (3) pie2 2439 4439 2539 4539 ---0 0000 ---0 0000 ---u uuuu ipr1 2439 4439 2539 4539 1111 1111 1111 1111 uuuu uuuu 2439 4439 2539 4539 -111 1111 -111 1111 -uuu uuuu pir1 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu (3) 2439 4439 2539 4539 -000 0000 -000 0000 -uuu uuuu (3) pie1 2439 4439 2539 4539 0000 0000 0000 0000 uuuu uuuu 2439 4439 2539 4539 -000 0000 -000 0000 -uuu uuuu trise 2439 4439 2539 4539 0000 -111 0000 -111 uuuu -uuu trisd 2439 4439 2539 4539 1111 1111 1111 1111 uuuu uuuu trisc* 2439 4439 2539 4539 1111 1111 1111 1111 uuuu uuuu trisb 2439 4439 2539 4539 1111 1111 1111 1111 uuuu uuuu trisa (5,6) 2439 4439 2539 4539 -111 1111 (5) -111 1111 (5) -uuu uuuu (5) late 2439 4439 2539 4539 ---- -xxx ---- -uuu ---- -uuu latd 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu latc* 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu latb 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu lata (5,6) 2439 4439 2539 4539 -xxx xxxx (5) -uuu uuuu (5) -uuu uuuu (5) porte 2439 4439 2539 4539 ---- -000 ---- -000 ---- -uuu portd 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu portc* 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu portb 2439 4439 2539 4539 xxxx xxxx uuuu uuuu uuuu uuuu porta (5,6) 2439 4439 2539 4539 -x0x 0000 (5) -u0u 0000 (5) -uuu uuuu (5) table 3-3: initialization conditions for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as '0', q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. * these registers are retained to maintain compatibility with pic18fxx2 devices; however, one or more bits are reserved. users should not modify the value of these bits. see section 4.9.2 for details. note 1: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 4: see table 3-2 for reset value for specific condition. 5: bit 6 of porta, lata, and trisa are enabled in ecio and rcio oscillator modes only. in all other oscillator modes, they are disabled and read ?0?. 6: bit 6 of porta, lata and trisa are not available on all devices. when unimplemented, they are read ?0?.
pic18fxx39 ds30485a-page 30 preliminary ? 2002 microchip technology inc. figure 3-3: time-out sequence on power-up (mclr tied to v dd ) figure 3-4: time-out sequence on power-up (mclr not tied to v dd ): case 1 figure 3-5: time-out sequence on power-up (mclr not tied to v dd ): case 2 t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost
? 2002 microchip technology inc. preliminary ds30485a-page 31 pic18fxx39 figure 3-6: slow rise time (mclr tied to v dd ) figure 3-7: time-out sequence on por w/ pll enabled (mclr tied to v dd ) v dd mclr internal por pwrt time-out ost time-out internal reset 0v 1v 5v t pwrt t ost t pwrt t ost v dd mclr iinternal por pwrt time-out ost time-out internal reset pll time-out t pll note: t ost = 1024 clock cycles. t pll 2 ms max. first three stages of the pwrt timer.
pic18fxx39 ds30485a-page 32 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 33 pic18fxx39 4.0 memory organization there are three memory blocks in enhanced mcu devices. these memory blocks are: ? program memory ? data ram ? data eeprom data and program memory use separate busses, which allows for concurrent access of these blocks. additional detailed information for flash program memory and data eeprom is provided in section 5.0 and section 6.0, respectively. 4.1 program memory organization a 21-bit program counter is capable of addressing the 2-mbyte program memory space. accessing a location between the physically implemented memory and the top of the 2-mbyte range will cause a read of all ?0?s (a nop instruction). the pic18f2539 and pic18F4539 each have a total of 24 kbytes, or 12k of single word instructions of flash memory, from addresses 0000h to 5fffh. the next 8 kbytes beyond this space (from 6000h to 7fffh) are reserved for the motor control kernel; accessing locations in this range will return random information. the pic18f2439 and pic18f4439 each have 12 kbytes, or 6k of single word instructions of flash memory, from addresses 0000h to 2fffh. the next 4 kbytes of this space (from 3000h to 3fffh) are reserved for the motor control kernel; accessing locations in this range will return random information. the reset vector address for all devices is at 0000h, and the interrupt vector addresses are at 0008h and 0018h. the memory maps for the pic18fx439 and pic18fx539 devices are shown in figure 4-1. figure 4-1: program memory map and stack for pic18fxx39 devices note: the prompt motor control kernel is iden- tical for all pic18fxx39 devices, regard- less of the difference in reserved block size between pic18fx439 and pic18fx539 devices pc<20:0> stack level 1 ? stack level 31 reset vector low priority interrupt vector ? ? call,rcall,return retfie,retlw 21 0000h 0018h on-chip program memory high priority interrupt vector 0008h user memory space 1fffffh 4000h 3fffh read '0' 200000h 3000h 2fffh reserved reset vector low priority interrupt vector 0000h 0018h 8000h 7fffh on-chip program memory high priority interrupt vector 0008h read '0' 1fffffh 200000h note: size of memory areas not to scale. pic18fx439 devices pic18fx539 devices reserved 6000h 5fffh
pic18fxx39 ds30485a-page 34 preliminary ? 2002 microchip technology inc. 4.2 return address stack the return address stack allows any combination of up to 31 program calls and interrupts to occur. the pc (program counter) is pushed onto the stack when a call or rcall instruction is executed, or an interrupt is acknowledged. the pc value is pulled off the stack on a return, retlw or a retfie instruction. pclatu and pclath are not affected by any of the return or call instructions. the stack operates as a 31-word by 21-bit ram and a 5-bit stack pointer, with the stack pointer initialized to 00000b after all resets. there is no ram associated with stack pointer 00000b. this is only a reset value. during a call type instruction, causing a push onto the stack, the stack pointer is first incremented and the ram location pointed to by the stack pointer is written with the contents of the pc. during a return type instruction, causing a pop from the stack, the contents of the ram location pointed to by the stkptr are transferred to the pc and then the stack pointer is decremented. the stack space is not part of either program or data space. the stack pointer is readable and writable, and the address on the top of the stack is readable and writ- able through sfr registers. data can also be pushed to, or popped from the stack using the top-of-stack sfrs. status bits indicate if the stack pointer is at, or beyond the 31 levels provided. 4.2.1 top-of-stack access the top of the stack is readable and writable. three register locations, tosu, tosh and tosl hold the contents of the stack location pointed to by the stkptr register. this allows users to implement a software stack if necessary. after a call, rcall or interrupt, the software can read the pushed value by reading the tosu, tosh and tosl registers. these values can be placed on a user defined software stack. at return time, the software can replace the tosu, tosh and tosl and do a return. the user must disable the global interrupt enable bits during this time to prevent inadvertent stack operations. 4.2.2 return stack pointer (stkptr) the stkptr register contains the stack pointer value, the stkful (stack full) status bit, and the stkunf (stack underflow) status bits. register 4-1 shows the stkptr register. the value of the stack pointer can be 0 through 31. the stack pointer increments when val- ues are pushed onto the stack and decrements when values are popped off the stack. at reset, the stack pointer value will be ?0?. the user may read and write the stack pointer value. this feature can be used by a real-time operating system for return stack maintenance. after the pc is pushed onto the stack 31 times (without popping any values off the stack), the stkful bit is set. the stkful bit can only be cleared in software or by a por. the action that takes place when the stack becomes full depends on the state of the stvren (stack over- flow reset enable) configuration bit. refer to section 21.0 for a description of the device configura- tion bits. if stvren is set (default), the 31st push will push the (pc + 2) value onto the stack, set the stkful bit, and reset the device. the stkful bit will remain set and the stack pointer will be set to ?0?. if stvren is cleared, the stkful bit will be set on the 31st push and the stack pointer will increment to 31. any additional pushes will not overwrite the 31st push, and stkptr will remain at 31. when the stack has been popped enough times to unload the stack, the next pop will return a value of zero to the pc and sets the stkunf bit, while the stack pointer remains at ?0?. the stkunf bit will remain set until cleared in software or a por occurs. note: returning a value of zero to the pc on an underflow has the effect of vectoring the program to the reset vector, where the stack conditions can be verified and appropriate actions can be taken.
? 2002 microchip technology inc. preliminary ds30485a-page 35 pic18fxx39 register 4-1: stkptr register figure 4-2: return address stack and associated registers 4.2.3 push and pop instructions since the top-of-stack (tos) is readable and writable, the ability to push values onto the stack and pull values off the stack, without disturbing normal program execu- tion, is a desirable option. to push the current pc value onto the stack, a push instruction can be executed. this will increment the stack pointer and load the cur- rent pc value onto the stack. tosu, tosh and tosl can then be modified to place a return address on the stack. the ability to pull the tos value off of the stack and replace it with the value that was previously pushed onto the stack, without disturbing normal execution, is achieved by using the pop instruction. the pop instruc- tion discards the current tos by decrementing the stack pointer. the previous value pushed onto the stack then becomes the tos value. 4.2.4 stack full/underflow resets these resets are enabled by programming the stvren configuration bit. when the stvren bit is disabled, a full or underflow condition will set the appro- priate stkful or stkunf bit, but not cause a device reset. when the stvren bit is enabled, a full or underflow condition will set the appropriate stkful or stkunf bit and then cause a device reset. the stkful or stkunf bits are only cleared by the user software or a por reset. r/c-0 r/c-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 stkful stkunf ? sp4 sp3 sp2 sp1 sp0 bit 7 bit 0 bit 7 (1) stkful: stack full flag bit 1 = stack became full or overflowed 0 = stack has not become full or overflowed bit 6 (1) stkunf: stack underflow flag bit 1 = stack underflow occurred 0 = stack underflow did not occur bit 5 unimplemented: read as '0' bit 4-0 sp4:sp0: stack pointer location bits note 1: bit 7 and bit 6 can only be cleared in user software or by a por. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown 00011 0x001a34 11111 11110 11101 00010 00001 00000 00010 return address stack to p - o f - st a c k 0x000d58 tosl tosh tosu 0x34 0x1a 0x00 stkptr<4:0>
pic18fxx39 ds30485a-page 36 preliminary ? 2002 microchip technology inc. 4.3 fast register stack for pic18fxx39 devices, a ?fast interrupt return? option is available for high priority interrupts. a single level fast register stack is provided for the status, wreg and bsr registers; it is not readable or writable. when the processor vectors for an interrupt, the stack is loaded with the current value of the corresponding register. if the fast return instruction is used to return from the interrupt, the values in the registers are then loaded back into the working registers. 4.4 pcl, pclath and pclatu the program counter (pc) specifies the address of the instruction to fetch for execution. the pc is 21-bits wide. the low byte is called the pcl register. this reg- ister is readable and writable. the high byte is called the pch register. this register contains the pc<15:8> bits and is not directly readable or writable. updates to the pch register may be performed through the pclath register. the upper byte is called pcu. this register contains the pc<20:16> bits and is not directly readable or writable. updates to the pcu register may be performed through the pclatu register. the pc addresses bytes in the program memory. to prevent the pc from becoming misaligned with word instructions, the lsb of pcl is fixed to a value of ?0?. the pc increments by 2 to address sequential instructions in the program memory. the call, rcall, goto and program branch instructions write to the program counter directly. for these instructions, the contents of pclath and pclatu are not transferred to the program counter. the contents of pclath and pclatu will be trans- ferred to the program counter by an operation that writes pcl. similarly, the upper two bytes of the pro- gram counter will be transferred to pclath and pclatu by an operation that reads pcl. this is useful for computed offsets to the pc (see section 4.8.1). 4.5 clocking scheme/instruction cycle the clock input (from osc1) is internally divided by four to generate four non-overlapping quadrature clocks, namely q1, q2, q3 and q4. internally, the pro- gram counter (pc) is incremented every q1, the instruction is fetched from the program memory and latched into the instruction register in q4. the instruc- tion is decoded and executed during the following q1 through q4. the clocks and instruction execution flow are shown in figure 4-3. figure 4-3: clock/instruction cycle note: the fast interrupt return for pic18fxx39 devices is reserved for use by the prompt kernel and the timer2 match interrupt. it is not available to the user for any other interrupts or returns from subroutines. q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 q1 q2 q3 q4 pc osc2/clko (rc mode) pc pc+2 pc+4 fetch inst (pc) execute inst (pc-2) fetch inst (pc+2) execute inst (pc) fetch inst (pc+4) execute inst (pc+2) internal phase clock
? 2002 microchip technology inc. preliminary ds30485a-page 37 pic18fxx39 4.6 instruction flow/pipelining an ?instruction cycle? consists of four q cycles (q1, q2, q3 and q4). the instruction fetch and execute are pipelined such that fetch takes one instruction cycle, while decode and execute takes another instruction cycle. however, due to the pipelining, each instruction effectively executes in one cycle. if an instruction causes the program counter to change (e.g., goto ), then two cycles are required to complete the instruction (example 4-1). a fetch cycle begins with the program counter (pc) incrementing in q1. in the execution cycle, the fetched instruction is latched into the ?instruction register? (ir) in cycle q1. this instruction is then decoded and executed during the q2, q3, and q4 cycles. data memory is read during q2 (operand read) and written during q4 (destination write). example 4-1: instruction pipeline flow 4.7 instructions in program memory the program memory is addressed in bytes. instruc- tions are stored as two bytes or four bytes in program memory. the least significant byte of an instruction word is always stored in a program memory location with an even address (lsb = 0). figure 4-4 shows an example of how instruction words are stored in the pro- gram memory. to maintain alignment with instruction boundaries, the pc increments in steps of 2 and the lsb will always read ?0? (see section 4.4). the call and goto instructions have an absolute pro- gram memory address embedded into the instruction. since instructions are always stored on word bound- aries, the data contained in the instruction is a word address. the word address is written to pc<20:1>, which accesses the desired byte address in program memory. instruction #2 in figure 4-4 shows how the instruction, ? goto 000006h ?, is encoded in the pro- gram memory. program branch instructions, which encode a relative address offset, operate in the same manner. the offset value stored in a branch instruction represents the number of single word instructions that the pc will be offset by. section 21.0 provides further details of the instruction set. figure 4-4: instructions in program memory all instructions are single cycle, except for any program branches. these take two cycles since the fetch instruction is ?flushed? from the pipeline while the new instruction is being fetched and then executed. t cy 0t cy 1t cy 2t cy 3t cy 4t cy 5 1. movlw 55h fetch 1 execute 1 2. movwf portb fetch 2 execute 2 3. bra sub_1 fetch 3 execute 3 4. bsf porta, bit3 (forced nop) fetch 4 flush ( nop ) 5. instruction @ address sub_1 fetch sub_1 execute sub_1 word address lsb = 1 lsb = 0 program memory byte locations 000000h 000002h 000004h 000006h instruction 1: movlw 055h 0fh 55h 000008h instruction 2: goto 000006h efh 03h 00000ah f0h 00h 00000ch instruction 3: movff 123h, 456h c1h 23h 00000eh f4h 56h 000010h 000012h 000014h
pic18fxx39 ds30485a-page 38 preliminary ? 2002 microchip technology inc. 4.7.1 two-word instructions the pic18fxx39 devices have four two-word instruc- tions: movff, call, goto and lfsr . the second word of these instructions has the 4 msbs set to ?1?s and is a special kind of nop instruction. the lower 12 bits of the second word contain data to be used by the instruction. if the first word of the instruction is exe- cuted, the data in the second word is accessed. if the second word of the instruction is executed by itself (first word was skipped), it will execute as a nop . this action is necessary when the two-word instruction is preceded by a conditional instruction that changes the pc. a pro- gram example that demonstrates this concept is shown in example 4-2. refer to section 21.0 for further details of the instruction set. example 4-2: two-word instructions 4.8 lookup tables lookup tables are implemented two ways. these are: ? computed goto ? table reads 4.8.1 computed goto a computed goto is accomplished by adding an offset to the program counter ( addwf pcl ). a lookup table can be formed with an addwf pcl instruction and a group of retlw 0xnn instructions. wreg is loaded with an offset into the table before executing a call to that table. the first instruction of the called routine is the addwf pcl instruction. the next instruction executed will be one of the retlw 0xnn instructions, that returns the value 0xnn to the calling function. the offset value (value in wreg) specifies the number of bytes that the program counter should advance. in this method, only one data byte may be stored in each instruction location and room on the return address stack is required. 4.8.2 table reads/table writes a better method of storing data in program memory allows 2 bytes of data to be stored in each instruction location. lookup table data may be stored 2 bytes per program word by using table reads and writes. the table pointer (tblptr) specifies the byte address and the table latch (tablat) contains the data that is read from, or written to program memory. data is transferred to/from program memory, one byte at a time. a description of the table read/table write operation is shown in section 5.1. case 1: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; no, execute 2-word instruction 1111 0100 0101 0110 ; 2nd operand holds address of reg2 0010 0100 0000 0000 addwf reg3 ; continue code case 2: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; yes 1111 0100 0101 0110 ; 2nd operand becomes nop 0010 0100 0000 0000 addwf reg3 ; continue code note: the addwf pcl instruction does not update pclath and pclatu. a read operation on pcl must be performed to update pclath and pclatu.
? 2002 microchip technology inc. preliminary ds30485a-page 39 pic18fxx39 4.9 data memory organization the data memory is implemented as static ram. each register in the data memory has a 12-bit address, allowing up to 4096 bytes of data memory. the data memory map is divided into 16 banks that contain 256 bytes each. the lower 4 bits of the bank select regis- ter (bsr<3:0>) select which bank will be accessed. the upper 4 bits for the bsr are not implemented. the data memory contains special function registers (sfrs) and general purpose registers (gprs). the sfrs are used for control and status of the controller and peripheral functions, while gprs are used for data storage and scratch pad operations in the user?s appli- cation. the sfrs start at the last location of bank 15 (fffh) and extend downwards. any remaining space beyond the sfrs in the bank may be implemented as gprs. gprs start at the first location of bank 0 and grow upwards. any read of an unimplemented location will read as ?0?s. the organization of the data memory space for these devices is shown in figure 4-5 and figure 4-6. pic18fx439 devices have 640 bytes of data ram, extending from bank 0 to bank 2 (000h through 27fh). the block of 128 bytes above this to the top of the bank (280h to 2ffh) is used as data memory for the motor control kernel, and is not available to the user. reading these locations will return random information that reflects the kernel?s ?scratch? data. modifying the data in these locations may disrupt the operation of the prompt kernel. pic18fx539 devices have 1408 bytes of data ram, extending from bank 0 to bank 5 (000h through 57fh). as with the pic18fx439 devices, the block of 128 bytes above this to the end of the bank (580h to 5ffh) is used by the motor control kernel. the entire data memory may be accessed directly or indirectly. direct addressing may require the use of the bsr register. indirect addressing requires the use of a file select register (fsrn) and a corresponding indi- rect file operand (indfn). each fsr holds a 12-bit address value that can be used to access any location in the data memory map without banking. the instruction set and architecture allow operations across all banks. this may be accomplished by indirect addressing, or by the use of the movff instruction. the movff instruction is a two-word/two-cycle instruction that moves a value from one register to another. to ensure that commonly used registers (sfrs and select gprs) can be accessed in a single cycle, regardless of the current bsr values, an access bank is implemented. a segment of bank 0 and a segment of bank 15 comprise the access ram. section 4.10 provides a detailed description of the access ram. 4.9.1 general purpose register file the register file can be accessed either directly or indi- rectly. indirect addressing operates using a file select register and corresponding indirect file operand. the operation of indirect addressing is shown in section 4.12. enhanced mcu devices may have banked memory in the gpr area. gprs are not initialized by a power-on reset and are unchanged on all other resets. data ram is available for use as gpr registers by all instructions. the top half of bank 15 (f80h to fffh) contains sfrs. all other banks of data memory contain gpr registers, starting with bank 0. 4.9.2 special function registers the special function registers (sfrs) are registers used by the cpu and peripheral modules for control- ling the desired operation of the device. these regis- ters are implemented as static ram. a list of these registers is given in table 4-1 and table 4-2. the sfrs can be classified into two sets; those asso- ciated with the ?core? function and those related to the peripheral functions. those registers related to the ?core? are described in this section, while those related to the operation of the peripheral features are described in the section of that peripheral feature. the sfrs are typically distributed among the peripherals whose functions they control. the unused sfr locations will be unimplemented and read as '0's. see table 4-1 for addresses for the sfrs. note: in this chapter and throughout this docu- ment, certain sfr names and individual bits are marked with an asterisk (*). this denotes registers that are not implemented in pic18fxx39 devices, but whose names are retained to maintain compatibility with pic18fxx2 devices. the designated bits within these registers are reserved and may be used by certain modules or the motor control kernel. users should not write to these registers or alter these bit values. failure to do this may result in erratic microcontroller operation.
pic18fxx39 ds30485a-page 40 preliminary ? 2002 microchip technology inc. figure 4-5: data memory map for pic18fx439 bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 080h 07fh f80h fffh 00h 7fh 80h ffh access bank when a = 0, the bsr is ignored and the access bank is used. the first 128 bytes are general purpose ram (from bank 0). the second 128 bytes are special function registers (from bank 15). when a = 1, the bsr is used to specify the ram location that the instruction uses. f7fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr gpr sfr unused access ram high access ram low bank 3 to 200h unused read ?00h? = 1110 = 0011 (sfrs) gpr 2ffh 300h ffh 00h bank 2 = 0010 27fh 280h prompt memory ? ? ?
? 2002 microchip technology inc. preliminary ds30485a-page 41 pic18fxx39 figure 4-6: data memory map for pic18fx539 bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1110 = 1111 080h 07fh f80h fffh 00h 7fh 80h ffh access bank when a = 0, the bsr is ignored and the access bank is used. the first 128 bytes are general purpose ram (from bank 0). the second 128 bytes are special function registers (from bank 15). when a = 1, the bsr is used to specify the ram location that the instruction uses. bank 4 bank 3 bank 2 f7fh f00h effh 3ffh 300h 2ffh 200h 1ffh 100h 0ffh 000h = 0110 = 0101 = 0011 = 0010 access ram ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h gpr gpr gpr gpr sfr unused access ram high access ram low bank 5 gpr gpr bank 6 to 4ffh 400h 5ffh 500h 600h unused read ?00h? = 0100 (sfrs) ? ? ? prompt memory
pic18fxx39 ds30485a-page 42 preliminary ? 2002 microchip technology inc. table 4-1: special function register map address name address name address name address name fffh tosu fdfh indf2 (3) fbfh ccpr1h f9fh ipr1 ffeh tosh fdeh postinc2 (3) fbeh ccpr1l * f9eh pir1 ffdh tosl fddh postdec2 (3) fbdh ccp1con * f9dh pie1 ffch stkptr fdch preinc2 (3) fbch ccpr2h f9ch ? ffbh pclatu fdbh plusw2 (3) fbbh ccpr2l * f9bh ? ffah pclath fdah fsr2h fbah ccp2con * f9ah ? ff9h pcl fd9h fsr2l fb9h ? f99h ? ff8h tblptru fd8h status fb8h ? f98h ? ff7h tblptrh fd7h tmr0h fb7h ? f97h ? ff6h tblptrl fd6h tmr0l fb6h ? f96h trise (2) ff5h tablat fd5h t0con fb5h ? f95h trisd (2) ff4h prodh fd4h ? fb4h ? f94h trisc (4) ff3h prodl fd3h osccon * fb3h tmr3h f93h trisb ff2h intcon fd2h lvdcon fb2h tmr3l f92h trisa ff1h intcon2 fd1h wdtcon fb1h t3con f91h ? ff0h intcon3 fd0h rcon fb0h ? f90h ? fefh indf0 (3) fcfh tmr1h fafh spbrg f8fh ? feeh postinc0 (3) fceh tmr1l faeh rcreg f8eh ? fedh postdec0 (3) fcdh t1con fadh txreg f8dh late (2) fech preinc0 (3) fcch tmr2 * fach txsta f8ch latd (2) febh plusw0 (3) fcbh pr2 * fabh rcsta f8bh latc (4) feah fsr0h fcah t2con * faah ? f8ah latb fe9h fsr0l fc9h sspbuf fa9h eeadr f89h lata fe8h wreg fc8h sspadd fa8h eedata f88h ? fe7h indf1 (3) fc7h sspstat fa7h eecon2 f87h ? fe6h postinc1 (3) fc6h sspcon1 fa6h eecon1 f86h ? fe5h postdec1 (3) fc5h sspcon2 fa5h ? f85h ? fe4h preinc1 (3) fc4h adresh fa4h ? f84h porte (2) fe3h plusw1 (3) fc3h adresl fa3h ? f83h portd (2) fe2h fsr1h fc2h adcon0 fa2h ipr2 f82h portc (4) fe1h fsr1l fc1h adcon1 fa1h pir2 f81h portb fe0h bsr fc0h ? fa0h pie2 f80h porta * these registers are retained to maintain compatibility with pic18fxx2 devices; however, one or more bits are reserved in pic18fxx39 devices. users should not alter the values of these bits. note 1: unimplemented registers are read as ?0?. 2: this register is not available on pic18f2x39 devices. 3: this is not a physical register. 4: bits 1 and 2 are reserved; users should not alter their values.
? 2002 microchip technology inc. preliminary ds30485a-page 43 pic18fxx39 table 4-2: register file summary file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: tosu ? ? ? top-of-stack upper byte (tos<20:16>) ---0 0000 26, 34 tosh top-of-stack high byte (tos<15:8>) 0000 0000 26, 34 tosl top-of-stack low byte (tos<7:0>) 0000 0000 26, 34 stkptr stkful stkunf ? return stack pointer 00-0 0000 26, 35 pclatu ? ? ? holding register for pc<20:16> ---0 0000 26, 36 pclath holding register for pc<15:8> 0000 0000 26, 36 pcl pc low byte (pc<7:0>) 0000 0000 26, 36 tblptru ? ?bit21 (2) program memory table pointer upper byte (tblptr<20:16>) --00 0000 26, 54 tblptrh program memory table pointer high byte (tblptr<15:8>) 0000 0000 26, 54 tblptrl program memory table pointer low byte (tblptr<7:0>) 0000 0000 26, 54 tablat program memory table latch 0000 0000 26, 54 prodh product register high byte xxxx xxxx 26, 67 prodl product register low byte xxxx xxxx 26, 67 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 26, 71 intcon2 rbpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip 1111 -1-1 26, 72 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 11-0 0-00 26, 73 indf0 uses contents of fsr0 to address data memory - value of fsr0 not changed (not a physical register) n/a 26, 47 postinc0 uses contents of fsr0 to address data memory - value of fsr0 post-incremented (not a physical register) n/a 26, 47 postdec0 uses contents of fsr0 to address data memory - value of fsr0 post-decremented (not a physical register) n/a 26, 47 preinc0 uses contents of fsr0 to address data memory - value of fsr0 pre-incremented (not a physical register) n/a 26, 47 plusw0 uses contents of fsr0 to address data memory - value of fsr0 (not a physical register). offset by value in wreg. n/a 26, 47 fsr0h ? ? ? ? indirect data memory address pointer 0 high byte ---- 0000 26, 47 fsr0l indirect data memory address pointer 0 low byte xxxx xxxx 26, 47 wreg working register xxxx xxxx 26 indf1 uses contents of fsr1 to address data memory - value of fsr1 not changed (not a physical register) n/a 26, 47 postinc1 uses contents of fsr1 to address data memory - value of fsr1 post-incremented (not a physical register) n/a 26, 47 postdec1 uses contents of fsr1 to address data memory - value of fsr1 post-decremented (not a physical register) n/a 26, 47 preinc1 uses contents of fsr1 to address data memory - value of fsr1 pre-incremented (not a physical register) n/a 26, 47 plusw1 uses contents of fsr1 to address data memory - value of fsr1 (not a physical register). offset by value in wreg. n/a 26, 47 fsr1h ? ? ? ? indirect data memory address pointer 1 high byte ---- 0000 27, 47 fsr1l indirect data memory address pointer 1 low byte xxxx xxxx 27, 47 bsr ? ? ? ? bank select register ---- 0000 27, 46 indf2 uses contents of fsr2 to address data memory - value of fsr2 not changed (not a physical register) n/a 27, 47 postinc2 uses contents of fsr2 to address data memory - value of fsr2 post-incremented (not a physical register) n/a 27, 47 postdec2 uses contents of fsr2 to address data memory - value of fsr2 post-decremented (not a physical register) n/a 27, 47 preinc2 uses contents of fsr2 to address data memory - value of fsr2 pre-incremented (not a physical register) n/a 27, 47 plusw2 uses contents of fsr2 to address data memory - value of fsr2 (not a physical register). offset by value in wreg. n/a 27, 47 fsr2h ? ? ? ? indirect data memory address pointer 2 high byte ---- 0000 27, 47 fsr2l indirect data memory address pointer 2 low byte xxxx xxxx 27, 47 status ? ? ?novzdcc ---x xxxx 27, 49 legend: x = unknown, u = unchanged, - = unimplemented, q = value depends on condition * these registers (or individual bits) are retained to maintain compatibility with pic18fxx2 devices; however, the indicated bit s are reserved in pic18fxx39 devices. users should not alter the values of these bits. see section 4.9.2 for details. note 1: ra6 and associated bits are configured as port pins in rcio and ecio oscillator mode only and read '0' in all other oscillator modes. 2: bit 21 of the tblptru allows access to the device configuration bits. 3: these registers and bits are reserved on the pic18f2x39 devices; always maintain these clear.
pic18fxx39 ds30485a-page 44 preliminary ? 2002 microchip technology inc. tmr0h timer0 register high byte 0000 0000 27, 101 tmr0l timer0 register low byte xxxx xxxx 27, 101 t0con tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 1111 1111 27, 99 osccon * ? ? ? ? ? ? ? * ---- ---0 27 lvdcon ? ? irvst lvden lvdl3 lvdl2 lvdl1 lvdl0 --00 0101 27, 191 wdtcon ? ? ? ? ? ? ?swdte ---- ---0 27, 203 rcon ipen ? ?ri to pd por bor 0--1 11qq 25, 50, 80 tmr1h timer1 register high byte xxxx xxxx 27, 103 tmr1l timer1 register low byte xxxx xxxx 27, 103 t1con rd16 ? t1ckps1 t1ckps0 ? t1sync tmr1cs tmr1on 0-00 0000 27, 103 tmr2 * * * * * * * * * 0000 0000 27 pr2 * * * * * * * * * 1111 1111 27 t2con * * * * * * * * * -000 0000 27 sspbuf ssp receive buffer/transmit register xxxx xxxx 27, 125 sspadd ssp address register in i 2 c slave mode. ssp baud rate reload register in i 2 c master mode. 0000 0000 27, 134 sspstat smp cke d/a psr/w ua bf 0000 0000 27, 126 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 27, 127 sspcon2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 27, 137 adresh a/d result register high byte xxxx xxxx 187,188 adresl a/d result register low byte xxxx xxxx 187,188 adcon0 adcs1 adcs0 chs2 chs1 chs0 go/done ?adon 0000 00-0 28, 181 adcon1 adfm adcs2 ? ? pcfg3 pcfg2 pcfg1 pcfg0 00-- 0000 28, 182 ccpr1h pwm register1 high byte (read only) xxxx xxxx 28, 124 ccpr1l * * * * * * * * * xxxx xxxx 28, 124 ccp1con * ? ? * * * * * * --00 0000 28, 124 ccpr2h pwm register2 high byte (read only) xxxx xxxx 28, 124 ccpr2l * * * * * * * * * xxxx xxxx 28, 124 ccp2con * ? ? * * * * * * --00 0000 28, 124 tmr3h timer3 register high byte xxxx xxxx 28, 109 tmr3l timer3 register low byte xxxx xxxx 28, 109 t3con rd16 ? t3ckps1 t3ckps0 ? t3sync tmr3cs tmr3on 0000 0000 28, 109 spbrg usart1 baud rate generator 0000 0000 28, 168 rcreg usart1 receive register 0000 0000 28, 175, 178 txreg usart1 transmit register 0000 0000 28, 173, 176 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 28, 166 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 000x 28, 167 eeadr data eeprom address register 0000 0000 28, 61, 65 eedata data eeprom data register 0000 0000 28, 65 eecon2 data eeprom control register 2 (not a physical register) ---- ---- 28, 61, 65 eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 28, 62 table 4-2: register file summary (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, - = unimplemented, q = value depends on condition * these registers (or individual bits) are retained to maintain compatibility with pic18fxx2 devices; however, the indicated bit s are reserved in pic18fxx39 devices. users should not alter the values of these bits. see section 4.9.2 for details. note 1: ra6 and associated bits are configured as port pins in rcio and ecio oscillator mode only and read '0' in all other oscillator modes. 2: bit 21 of the tblptru allows access to the device configuration bits. 3: these registers and bits are reserved on the pic18f2x39 devices; always maintain these clear.
? 2002 microchip technology inc. preliminary ds30485a-page 45 pic18fxx39 ipr2 ? ? ? eeip bclip lvdip tmr3ip ? ---1 1111 29, 79 pir2 ? ? ? eeif bclif lvdif tmr3if ? ---0 0000 29, 75 pie2 ? ? ? eeie bclie lvdie tmr3ie ? ---0 0000 29, 77 ipr1 pspip (3) adip rcip txip sspip ? tmr2ip tmr1ip 1111 1111 29, 78 pir1 pspif (3) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 29, 74 pie1 pspie (3) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 29, 76 trise (3) ibf obf ibov pspmode ? data direction bits for porte 0000 -111 29, 94 trisd (3) data direction control register for portd 1111 1111 29, 92 trisc trisc7 trisc6 trisc5 trisc4 trisc3 * * trisc0 1111 1111 29, 89 trisb data direction control register for portb 1111 1111 29, 86 trisa ?trisa6 (1) data direction control register for porta -111 1111 29, 83 late (3) ? ? ? ? ? read porte data latch, write porte data latch ---- -xxx 29, 95 latd (3) read portd data latch, write portd data latch xxxx xxxx 29, 91 latc latc7 latc6 latc5 latc4 latc3 * * latc0 xxxx xxxx 29, 89 latb read portb data latch, write portb data latch xxxx xxxx 29, 86 lata ?lata6 (1) read porta data latch, write porta data latch (1) -xxx xxxx 29, 83 porte (3) read porte pins, write porte data latch ---- -000 29, 95 portd (3) read portd pins, write portd data latch xxxx xxxx 29, 91 portc rc7 rc6 rc5 rc4 rc3 * * rc0 xxxx xxxx 29, 89 portb read portb pins, write portb data latch xxxx xxxx 29, 86 porta ?ra6 (1) read porta pins, write porta data latch (1) -x0x 0000 29, 83 table 4-2: register file summary (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, - = unimplemented, q = value depends on condition * these registers (or individual bits) are retained to maintain compatibility with pic18fxx2 devices; however, the indicated bit s are reserved in pic18fxx39 devices. users should not alter the values of these bits. see section 4.9.2 for details. note 1: ra6 and associated bits are configured as port pins in rcio and ecio oscillator mode only and read '0' in all other oscillator modes. 2: bit 21 of the tblptru allows access to the device configuration bits. 3: these registers and bits are reserved on the pic18f2x39 devices; always maintain these clear.
pic18fxx39 ds30485a-page 46 preliminary ? 2002 microchip technology inc. 4.10 access bank the access bank is an architectural enhancement which is very useful for c compiler code optimization. the techniques used by the c compiler may also be useful for programs written in assembly. this data memory region can be used for: ? intermediate computational values ? local variables of subroutines ? faster context saving/switching of variables ? common variables ? faster evaluation/control of sfrs (no banking) the access bank is comprised of the upper 128 bytes in bank 15 (sfrs) and the lower 128 bytes in bank 0. these two sections will be referred to as access ram high and access ram low, respectively. figure 4-5 and figure 4-6 indicate the access ram areas. a bit in the instruction word specifies if the operation is to occur in the bank specified by the bsr register, or in the access bank. this bit is denoted by the ?a? bit (for access bit). when forced in the access bank (a = 0), the last address in access ram low is followed by the first address in access ram high. access ram high maps the special function registers, so that these registers can be accessed without any software overhead. this is useful for testing status flags and modifying control bits. 4.11 bank select register (bsr) the need for a large general purpose memory space dictates a ram banking scheme. the data memory is partitioned into sixteen banks. when using direct addressing, the bsr should be configured for the desired bank. bsr<3:0> holds the upper 4 bits of the 12-bit ram address. the bsr<7:4> bits will always read ?0?s, and writes will have no effect. a movlb instruction has been provided in the instruction set to assist in selecting banks. if the currently selected bank is not implemented, any read will return all ?0?s and all writes are ignored. the status register bits will be set/cleared as appropriate for the instruction performed. each bank extends up to ffh (256 bytes). all data memory is implemented as static ram. a movff instruction ignores the bsr, since the 12-bit addresses are embedded into the instruction word. section 4.12 provides a description of indirect address- ing, which allows linear addressing of the entire ram space. figure 4-7: direct addressing note 1: for register file map detail, see table 4-1. 2: the access bit of the instruction can be used to force an override of the selected bank (bsr<3:0>) to the registers of the access bank. 3: the movff instruction embeds the entire 12-bit address in the instruction. data memory (1) direct addressing bank select (2) location select (3) bsr<3:0> 7 0 from opcode (3) 00h 01h 0eh 0fh bank 0 bank 1 bank 14 bank 15 1ffh 100h 0ffh 000h effh e00h fffh f00h
? 2002 microchip technology inc. preliminary ds30485a-page 47 pic18fxx39 4.12 indirect addressing, indf and fsr registers indirect addressing is a mode of addressing data mem- ory, where the data memory address in the instruction is not fixed. an fsr register is used as a pointer to the data memory location that is to be read or written. since this pointer is in ram, the contents can be modified by the program. this can be useful for data tables in the data memory and for software stacks. figure 4-8 shows the operation of indirect addressing. this shows the moving of the value to the data memory address specified by the value of the fsr register. indirect addressing is possible by using one of the indf registers. any instruction using the indf register actually accesses the register pointed to by the file select register, fsr. reading the indf register itself, indirectly (fsr = 0), will read 00h. writing to the indf register indirectly, results in a nop operation. the fsr register contains a 12-bit address, which is shown in figure 4-9. the indfn register is not a physical register. address- ing indfn actually addresses the register whose address is contained in the fsrn register (fsrn is a pointer). this is indirect addressing. example 4-3 shows a simple use of indirect addressing to clear the ram in bank 1 (locations 100h-1ffh) in a minimum number of instructions. example 4-3: how to clear ram (bank 1) using indirect addressing there are three indirect addressing registers. to address the entire data memory space (4096 bytes), these registers are 12-bits wide. to store the 12 bits of addressing information, two 8-bit registers are required. these indirect addressing registers are: 1. fsr0: composed of fsr0h:fsr0l 2. fsr1: composed of fsr1h:fsr1l 3. fsr2: composed of fsr2h:fsr2l in addition, there are registers indf0, indf1 and indf2, which are not physically implemented. reading or writing to these registers activates indirect address- ing, with the value in the corresponding fsr register being the address of the data. if an instruction writes a value to indf0, the value will be written to the address pointed to by fsr0h:fsr0l. a read from indf1 reads the data from the address pointed to by fsr1h:fsr1l. indfn can be used in code anywhere an operand can be used. if indf0, indf1 or indf2 are read indirectly via an fsr, all '0's are read (zero bit is set). similarly, if indf0, indf1 or indf2 are written to indirectly, the operation will be equivalent to a nop instruction and the status bits are not affected. 4.12.1 indirect addressing operation each fsr register has an indf register associated with it, plus four additional register addresses. perform- ing an operation on one of these five registers deter- mines how the fsr will be modified during indirect addressing. when data access is done to one of the five indfn locations, the address selected will configure the fsrn register to: ? do nothing to fsrn after an indirect access (no change) - indfn ? auto-decrement fsrn after an indirect access (post-decrement) - postdecn ? auto-increment fsrn after an indirect access (post-increment) - postincn ? auto-increment fsrn before an indirect access (pre-increment) - preincn ? use the value in the wreg register as an offset to fsrn. do not modify the value of the wreg or the fsrn register after an indirect access (no change) - pluswn when using the auto-increment or auto-decrement fea- tures, the effect on the fsr is not reflected in the status register. for example, if the indirect address causes the fsr to equal '0', the z bit will not be set. incrementing or decrementing an fsr affects all 12 bits. that is, when fsrnl overflows from an increment, fsrnh will be incremented automatically. adding these features allows the fsrn to be used as a stack pointer, in addition to its uses for table operations in data memory. each fsr has an address associated with it that per- forms an indexed indirect access. when a data access to this indfn location (pluswn) occurs, the fsrn is configured to add the signed value in the wreg regis- ter and the value in fsr to form the address, before an indirect access. the fsr value is not changed. if an fsr register contains a value that points to one of the indfn, an indirect read will read 00h (zero bit is set), while an indirect write will be equivalent to a nop (status bits are not affected). if an indirect addressing operation is done where the target address is an fsrnh or fsrnl register, the write operation will dominate over the pre- or post-increment/decrement functions. lfsr fsr0 ,0x100 ; next clrf postinc0 ; clear indf ; register and ; inc pointer btfss fsr0h, 1 ; all done with ; bank1? goto next ; no, clear next continue ; yes, continue
pic18fxx39 ds30485a-page 48 preliminary ? 2002 microchip technology inc. figure 4-8: indirect addressing operation figure 4-9: indirect addressing opcode address file address = access of an indirect addressing register fsr instruction executed instruction fetched ram opcode file 12 12 12 bsr<3:0> 8 4 0h fffh note 1: for register file map detail, see table 4-1. data memory (1) indirect addressing fsr register 11 0 0fffh 0000h location select
? 2002 microchip technology inc. preliminary ds30485a-page 49 pic18fxx39 4.13 status register the status register, shown in register 4-2, contains the arithmetic status of the alu. the status register can be the destination for any instruction, as with any other register. if the status register is the destination for an instruction that affects the z, dc, c, ov, or n bits, then the write to these five bits is disabled. these bits are set or cleared according to the device logic. there- fore, the result of an instruction with the status register as destination may be different than intended. for example, clrf status will clear the upper three bits and set the z bit. this leaves the status register as 000u u1uu (where u = unchanged). it is recommended, therefore, that only bcf, bsf, swapf, movff and movwf instructions are used to alter the status register, because these instructions do not affect the z, c, dc, ov, or n bits from the status register. for other instructions not affecting any status bits, see table 21-2. register 4-2: status register note: the c and dc bits operate as a borrow and digit borrow bit respectively, in subtraction. u-0 u-0 u-0 r/w-x r/w-x r/w-x r/w-x r/w-x ? ? ?novzdcc bit 7 bit 0 bit 7-5 unimplemented: read as '0' bit 4 n: negative bit this bit is used for signed arithmetic (2?s complement). it indicates whether the result was negative (alu msb = 1). 1 = result was negative 0 = result was positive bit 3 ov: overflow bit this bit is used for signed arithmetic (2?s complement). it indicates an overflow of the 7-bit magnitude, which causes the sign bit (bit 7) to change state. 1 = overflow occurred for signed arithmetic (in this arithmetic operation) 0 = no overflow occurred bit 2 z: zero bit 1 = the result of an arithmetic or logic operation is zero 0 = the result of an arithmetic or logic operation is not zero bit 1 dc: digit carry/borrow bit for addwf, addlw, sublw , and subwf instructions 1 = a carry-out from the 4th low order bit of the result occurred 0 = no carry-out from the 4th low order bit of the result note: for borrow, the polarity is reversed. a subtraction is executed by adding the two?s complement of the second operand. for rotate ( rrf, rlf ) instructions, this bit is loaded with either the bit 4 or bit 3 of the source register. bit 0 c: carry/borrow bit for addwf, addlw, sublw , and subwf instructions 1 = a carry-out from the most significant bit of the result occurred 0 = no carry-out from the most significant bit of the result occurred note: for borrow, the polarity is reversed. a subtraction is executed by adding the two?s complement of the second operand. for rotate ( rrf, rlf ) instructions, this bit is loaded with either the high or low order bit of the source register. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 50 preliminary ? 2002 microchip technology inc. 4.14 rcon register the reset control (rcon) register contains flag bits that allow differentiation between the sources of a device reset. these flags include the to , pd , por , bor and ri bits. this register is readable and writable. for pic18fxx39 devices, the ipen bit must always be set (= 1 ) for the prompt kernel to function correctly. refer to section 8.0 (page 69) for a more detailed discussion. register 4-3: rcon register note 1: if the boren configuration bit is set (brown-out reset enabled), the bor bit is ?1? on a power-on reset. after a brown- out reset has occurred, the bor bit will be cleared, and must be set by firmware to indicate the occurrence of the next brown-out reset. 2: it is recommended that the por bit be set after a power-on reset has been detected, so that subsequent power-on resets may be detected. r/w-0 u-0 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen ? ?ri to pd por bor bit 7 bit 0 bit 7 ipen: interrupt priority enable bit always maintain this bit set for proper operation of prompt kernel. bit 6-5 unimplemented: read as '0' bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed 0 = the reset instruction was executed causing a device reset (must be set in software after a brown-out reset occurs) bit 3 to : watchdog time-out flag bit 1 = after power-up, clrwdt instruction, or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = after power-up or by the clrwdt instruction 0 = by execution of the sleep instruction bit 1 por : power-on reset status bit 1 = a power-on reset has not occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit 1 = a brown-out reset has not occurred 0 = a brown-out reset occurred (must be set in software after a brown-out reset occurs) legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 51 pic18fxx39 5.0 flash program memory the flash program memory is readable, writable, and erasable during normal operation over the entire v dd range. a read from program memory is executed on one byte at a time. a write to program memory is executed on blocks of 8 bytes at a time. program memory is erased in blocks of 64 bytes at a time. a bulk erase operation may not be issued from user code. writing or erasing program memory will cease instruc- tion fetches until the operation is complete. the pro- gram memory cannot be accessed during the write or erase, therefore, code cannot execute. an internal pro- gramming timer terminates program memory writes and erases. a value written to program memory does not need to be a valid instruction. executing a program memory location that forms an invalid instruction results in a nop . 5.1 table reads and table writes in order to read and write program memory, there are two operations that allow the processor to move bytes between the program memory space and the data ram: ? table read ( tblrd ) ? table write ( tblwt ) the program memory space is 16-bits wide, while the data ram space is 8-bits wide. table reads and table writes move data between these two memory spaces through an 8-bit register (tablat). table read operations retrieve data from program memory and places it into the data ram space. figure 5-1 shows the operation of a table read with program memory and data ram. table write operations store data from the data mem- ory space into holding registers in program memory. the procedure to write the contents of the holding reg- isters into program memory is detailed in section 5.5, ?writing to flash program memory?. figure 5-2 shows the operation of a table write with program memory and data ram. table operations work with byte entities. a table block containing data, rather than program instructions, is not required to be word aligned. therefore, a table block can start and end at any byte address. if a table write is being used to write executable code into program memory, program instructions will need to be word aligned. figure 5-1: table read operation table pointer (1) table latch (8-bit) program memory tblptrh tblptrl tablat tblptru instruction: tblrd * note 1: table pointer points to a byte in program memory. program memory (tblptr)
pic18fxx39 ds30485a-page 52 preliminary ? 2002 microchip technology inc. figure 5-2: table write operation 5.2 control registers several control registers are used in conjunction with the tblrd and tblwt instructions. these include the: ? eecon1 register ? eecon2 register ? tablat register ? tblptr registers 5.2.1 eecon1 and eecon2 registers eecon1 is the control register for memory accesses. eecon2 is not a physical register. reading eecon2 will read all '0's. the eecon2 register is used exclusively in the memory write and erase sequences. control bit eepgd determines if the access will be a program or data eeprom memory access. when clear, any subsequent operations will operate on the data eeprom memory. when set, any subsequent operations will operate on the program memory. control bit cfgs determines if the access will be to the configuration registers or to program memory/data eeprom memory. when set, subsequent operations will operate on configuration registers, regardless of eepgd (see section 20.0, ?special features of the cpu?). when clear, memory selection access is determined by eepgd. the free bit, when set, will allow a program memory erase operation. when the free bit is set, the erase operation is initiated on the next wr command. when free is clear, only writes are enabled. the wren bit, when set, will allow a write operation. on power-up, the wren bit is clear. the wrerr bit is set when a write operation is interrupted by a mclr reset, or a wdt time-out reset, during normal oper- ation. in these situations, the user can check the wrerr bit and rewrite the location. it is necessary to reload the data and address registers (eedata and eeadr), due to reset values of zero. control bit wr initiates write operations. this bit cannot be cleared, only set, in software. it is cleared in hard- ware at the completion of the write operation. the inability to clear the wr bit in software prevents the accidental or premature termination of a write operation. table pointer (1) table latch (8-bit) tblptrh tblptrl tablat program memory (tblptr) tblptru instruction: tblwt * note 1: table pointer actually points to one of eight holding registers, the address of which is determined by tblptrl<2:0>. the process for physically writing data to the program memory array is discussed in section 5.5. holding registers program memory note: interrupt flag bit, eeif in the pir2 register, is set when the write is complete. it must be cleared in software.
? 2002 microchip technology inc. preliminary ds30485a-page 53 pic18fxx39 register 5-1: eecon1 register (address fa6h) r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data ee or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as '0' bit 4 free: flash row erase enable bit 1 = erase the program memory row addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write only bit 3 wrerr: flash program/data ee error flag bit 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation) 0 = the write operation completed note: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition. bit 2 wren: flash program/data ee write enable bit 1 = allows write cycles 0 = inhibits write to the eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) in software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared in hardware. the rd bit can only be set (not cleared) in software. rd bit cannot be set when eepgd = 1.) 0 = does not initiate an eeprom read legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 54 preliminary ? 2002 microchip technology inc. 5.2.2 tablat - table latch register the table latch (tablat) is an 8-bit register mapped into the sfr space. the table latch is used to hold 8-bit data during data transfers between program memory and data ram. 5.2.3 tblptr - table pointer register the table pointer (tblptr) addresses a byte within the program memory. the tblptr is comprised of three sfr registers: table pointer upper byte, table pointer high byte and table pointer low byte (tblptru:tblptrh:tblptrl). these three regis- ters join to form a 22-bit wide pointer. the low order 21 bits allow the device to address up to 2 mbytes of pro- gram memory space. the 22nd bit allows access to the device id, the user id and the configuration bits. the table pointer, tblptr, is used by the tblrd and tblwt instructions. these instructions can update the tblptr in one of four ways based on the table opera- tion. these operations are shown in table 5-1. these operations on the tblptr only affect the low order 21 bits. 5.2.4 table pointer boundaries tblptr is used in reads, writes, and erases of the flash program memory. when a tblrd is executed, all 22 bits of the table pointer determine which byte is read from program memory into tablat. when a tblwt is executed, the three lsbs of the table pointer (tblptr<2:0>) determine which of the eight program memory holding registers is written to. when the timed write to program memory (long write) begins, the 19 msbs of the table pointer, tblptr (tblptr<21:3>), will determine which program mem- ory block of 8 bytes is written to. for more detail, see section 5.5 (?writing to flash program memory?). when an erase of program memory is executed, the 16 msbs of the table pointer (tblptr<21:6>) point to the 64-byte block that will be erased. the least significant bits (tblptr<5:0>) are ignored. figure 5-3 describes the relevant boundaries of tblptr based on flash program memory operations. table 5-1: table pointer operations with tblrd and tblwt instructions figure 5-3: table pointer boundaries based on operation example operation on table pointer tblrd* tblwt* tblptr is not modified tblrd*+ tblwt*+ tblptr is incremented after the read/write tblrd*- tblwt*- tblptr is decremented after the read/write tblrd+* tblwt+* tblptr is incremented before the read/write 21 16 15 87 0 erase - tblptr<21:6> write - tblptr<21:3> read - tblptr<21:0> tblptrl tblptrh tblptru
? 2002 microchip technology inc. preliminary ds30485a-page 55 pic18fxx39 5.3 reading the flash program memory the tblrd instruction is used to retrieve data from pro- gram memory and place into data ram. table reads from program memory are performed one byte at a time. tblptr points to a byte address in program space. executing tblrd places the byte pointed to into tablat. in addition, tblptr can be modified automatically for the next table read operation. the internal program memory is typically organized by words. the least significant bit of the address selects between the high and low bytes of the word. figure 5-4 shows the interface between the internal program memory and the tablat. figure 5-4: reads from flash program memory example 5-1: reading a flash program memory word (even byte address) program memory (odd byte address) tblrd tablat tblptr = xxxxx1 fetch instruction register (ir) read register tblptr = xxxxx0 movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the word movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_word tblrd*+ ; read into tablat and increment movf tablat, w ; get data movwf word_even tblrd*+ ; read into tablat and increment movf tablat, w ; get data movwf word_odd
pic18fxx39 ds30485a-page 56 preliminary ? 2002 microchip technology inc. 5.4 erasing flash program memory the minimum erase block is 32 words or 64 bytes. only through the use of an external programmer, or through icsp control can larger blocks of program memory be bulk erased. word erase in the flash array is not supported. when initiating an erase sequence from the micro- controller itself, a block of 64 bytes of program memory is erased. the most significant 16 bits of the tblptr<21:6> point to the block being erased; tblptr<5:0> are ignored. the eecon1 register commands the erase operation. the eepgd bit must be set to point to the flash pro- gram memory. the wren bit must be set to enable write operations. the free bit is set to select an erase operation. for protection, the write initiate sequence for eecon2 must be used. a long write is necessary for erasing the internal flash. instruction execution is halted while in a long write cycle. the long write will be terminated by the internal programming timer. 5.4.1 flash program memory erase sequence the sequence of events for erasing a block of internal program memory location is: 1. load table pointer with address of row being erased. 2. set eepgd bit to point to program memory, clear cfgs bit to access program memory, set wren bit to enable writes, and set free bit to enable the erase. 3. disable interrupts. 4. write 55h to eecon2. 5. write aah to eecon2. 6. set the wr bit. this will begin the row erase cycle. 7. the cpu will stall for duration of the erase (about 2 ms using internal timer). 8. re-enable interrupts. example 5-2: erasing a flash program memory row movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl erase_row bsf eecon1,eepgd ; point to flash program memory bcf eecon1,cfgs ; access flash program memory bsf eecon1,wren ; enable write to memory bsf eecon1,free ; enable row erase operation bcf intcon,gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw aah movwf eecon2 ; write aah bsf eecon1,wr ; start erase (cpu stall) bsf intcon,gie ; re-enable interrupts
? 2002 microchip technology inc. preliminary ds30485a-page 57 pic18fxx39 5.5 writing to flash program memory the minimum programming block is 4 words or 8 bytes. word or byte programming is not supported. table writes are used internally to load the holding reg- isters needed to program the flash memory. there are 8 holding registers used by the table writes for programming. since the table latch (tablat) is only a single byte, the tblwt instruction has to be executed 8 times for each programming operation. all of the table write operations will essentially be short writes, because only the holding registers are written. at the end of updating 8 registers, the eecon1 register must be written to, to start the programming operation with a long write. the long write is necessary for programming the inter- nal flash. instruction execution is halted while in a long write cycle. the long write will be terminated by the internal programming timer. the eeprom on-chip timer controls the write time. the write/erase voltages are generated by an on-chip charge pump rated to operate over the voltage range of the device for byte or word operations. figure 5-5: table writes to flash program memory 5.5.1 flash program memory write sequence the sequence of events for programming an internal program memory location should be: 1. read 64 bytes into ram. 2. update data values in ram as necessary. 3. load table pointer with address being erased. 4. do the row erase procedure. 5. load table pointer with address of first byte being written. 6. write the first 8 bytes into the holding registers with auto-increment ( tblwt*+ or tblwt+* ). 7. set eepgd bit to point to program memory, clear the cfgs bit to access program memory, and set wren to enable byte writes. 8. disable interrupts. 9. write 55h to eecon2. 10. write aah to eecon2. 11. set the wr bit. this will begin the write cycle. 12. the cpu will stall for duration of the write (about 2 ms using internal timer). 13. re-enable interrupts. 14. repeat steps 6-14 seven times, to write 64 bytes. 15. verify the memory (table read). this procedure will require about 18 ms to update one row of 64 bytes of memory. an example of the required code is given in example 5-3. holding register tablat holding register tblptr = xxxxx7 holding register tblptr = xxxxx1 holding register tblptr = xxxxx0 8 8 8 8 write register tblptr = xxxxx2 program memory note: before setting the wr bit, the table pointer address needs to be within the intended address range of the 8 bytes in the holding registers.
pic18fxx39 ds30485a-page 58 preliminary ? 2002 microchip technology inc. example 5-3: writing to flash program memory movlw d'64 ; number of bytes in erase block movwf counter movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_block tblrd*+ ; read into tablat, and inc movf tablat, w ; get data movwf postinc0 ; store data decfsz counter ; done? bra read_block ; repeat modify_word movlw data_addr_high ; point to buffer movwf fsr0h movlw data_addr_low movwf fsr0l movlw new_data_low ; update buffer word movwf postinc0 movlw new_data_high movwf indf0 erase_block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl bsf eecon1,eepgd ; point to flash program memory bcf eecon1,cfgs ; access flash program memory bsf eecon1,wren ; enable write to memory bsf eecon1,free ; enable row erase operation bcf intcon,gie ; disable interrupts movlw 55h movwf eecon2 ; write 55h movlw aah movwf eecon2 ; write aah bsf eecon1,wr ; start erase (cpu stall) bsf intcon,gie ; re-enable interrupts tblrd*- ; dummy read decrement write_buffer_back movlw 8 ; number of write buffer groups of 8 bytes movwf counter_hi movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l program_loop movlw 8 ; number of bytes in holding register movwf counter write_word_to_hregs movf postinc0, w ; get low byte of buffer data movwf tablat ; present data to table latch tblwt+* ; write data, perform a short write ; to internal tblwt holding register. decfsz counter ; loop until buffers are full bra write_word_to_hregs
? 2002 microchip technology inc. preliminary ds30485a-page 59 pic18fxx39 example 5-3: writing to flash program memory (continued) 5.5.2 write verify depending on the application, good programming practice may dictate that the value written to the mem- ory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. 5.5.3 unexpected termination of write operation if a write is terminated by an unplanned event, such as loss of power or an unexpected reset, the memory location just programmed should be verified and repro- grammed if needed.the wrerr bit is set when a write operation is interrupted by a mclr reset, or a wdt time-out reset during normal operation. in these situ- ations, users can check the wrerr bit and rewrite the location. 5.5.4 protection against spurious writes to protect against spurious writes to flash program memory, the write initiate sequence must also be fol- lowed. see ?special features of the cpu? (section 20.0) for more detail. 5.6 flash program operation during code protection see ?special features of the cpu? (section 20.0) for details on code protection of flash program memory. table 5-2: registers associated with program flash memory program_memory bsf eecon1,eepgd ; point to flash program memory bcf eecon1,cfgs ; access flash program memory bsf eecon1,wren ; enable write to memory bcf intcon,gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw aah movwf eecon2 ; write aah bsf eecon1,wr ; start program (cpu stall) bsf intcon,gie ; re-enable interrupts decfsz counter_hi ; loop until done bra program_loop bcf eecon1,wren ; disable write to memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets tblptru ? ? bit 21 program memory table pointer upper byte (tblptr<20:16>) --00 0000 --00 0000 tbpltrh program memory table pointer high byte (tblptr<15:8>) 0000 0000 0000 0000 tblptrl program memory table pointer high byte (tblptr<7:0>) 0000 0000 0000 0000 tablat program memory table latch 0000 0000 0000 0000 intcon gie/gieh peie/giel tmr0ie inte rbie tmr0if intf rbif 0000 000x 0000 000u eecon2 eeprom control register2 (not a physical register) ? ? eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 uu-0 u000 ipr2 ? ? ? eeip bclip lvdip tmr3ip ? ---1 1111 ---1 1111 pir2 ? ? ? eeif bclif lvdif tmr3if ? ---0 0000 ---0 0000 pie2 ? ? ?eeie bclie lvdie tmr3ie ? ---0 0000 ---0 0000 legend: x = unknown, u = unchanged, - = unimplemented read as '0'. shaded cells are not used during flash/eeprom access.
pic18fxx39 ds30485a-page 60 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 61 pic18fxx39 6.0 data eeprom memory the data eeprom is readable and writable during normal operation over the entire v dd range. the data memory is not directly mapped in the register file space. instead, it is indirectly addressed through the special function registers (sfr). there are four sfrs used to read and write the program and data eeprom memory. these registers are: ? eecon1 ? eecon2 ? eedata ? eeadr the eeprom data memory allows byte read and write. when interfacing to the data memory block, eedata holds the 8-bit data for read/write and eeadr holds the address of the eeprom location being accessed. these devices have 256 bytes of data eeprom with an address range from 0h to ffh. the eeprom data memory is rated for high erase/ write cycles. a byte write automatically erases the loca- tion and writes the new data (erase-before-write). the write time is controlled by an on-chip timer. the write time will vary with voltage and temperature, as well as from chip to chip. please refer to parameter d122 (electrical characteristics, section 23.0) for exact limits. 6.1 eeadr the address register can address up to a maximum of 256 bytes of data eeprom. 6.2 eecon1 and eecon2 registers eecon1 is the control register for eeprom memory accesses. eecon2 is not a physical register. reading eecon2 will read all '0's. the eecon2 register is used exclusively in the eeprom write sequence. control bits rd and wr initiate read and write opera- tions, respectively. these bits cannot be cleared, only set, in software. they are cleared in hardware at the completion of the read or write operation. the inability to clear the wr bit in software prevents the accidental or premature termination of a write operation. the wren bit, when set, will allow a write operation. on power-up, the wren bit is clear. the wrerr bit is set when a write operation is interrupted by a mclr reset, or a wdt time-out reset during normal opera- tion. in these situations, the user can check the wrerr bit and rewrite the location. it is necessary to reload the data and address registers (eedata and eeadr), due to the reset condition forcing the contents of the registers to zero. note: interrupt flag bit, eeif in the pir2 register, is set when write is complete. it must be cleared in software.
pic18fxx39 ds30485a-page 62 preliminary ? 2002 microchip technology inc. register 6-1: eecon1 register (address fa6h) r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data ee or configuration select bit 1 = access configuration or calibration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as '0' bit 4 free: flash row erase enable bit 1 = erase the program memory row addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write only bit 3 wrerr: flash program/data ee error flag bit 1 = a write operation is prematurely terminated (any mclr or any wdt reset during self-timed programming in normal operation) 0 = the write operation completed note: when a wrerr occurs, the eepgd or free bits are not cleared. this allows tracing of the error condition. bit 2 wren: flash program/data ee write enable bit 1 = allows write cycles 0 = inhibits write to the eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) in software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared in hardware. the rd bit can only be set (not cleared) in software. rd bit cannot be set when eepgd = 1.) 0 = does not initiate an eeprom read legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 63 pic18fxx39 6.3 reading the data eeprom memory to read a data memory location, the user must write the address to the eeadr register, clear the eepgd con- trol bit (eecon1<7>), clear the cfgs control bit (eecon1<6>), and then set control bit rd (eecon1<0>). the data is available for the very next instruction cycle; therefore, the eedata register can be read by the next instruction. eedata will hold this value until another read operation, or until it is written to by the user (during a write operation). example 6-1: data eeprom read 6.4 writing to the data eeprom memory to write an eeprom data location, the address must first be written to the eeadr register and the data writ- ten to the eedata register. then, the sequence in example 6-2 must be followed to initiate the write cycle. the write will not initiate if the above sequence is not exactly followed (write 55h to eecon2, write aah to eecon2, then set wr bit) for each byte. it is strongly recommended that interrupts be disabled during this code segment. additionally, the wren bit in eecon1 must be set to enable writes. this mechanism prevents accidental writes to data eeprom due to unexpected code exe- cution (i.e., runaway programs). the wren bit should be kept clear at all times, except when updating the eeprom. the wren bit is not cleared by hardware. after a write sequence has been initiated, eecon1, eeadr and edata cannot be modified. the wr bit will be inhibited from being set unless the wren bit is set. the wren bit must be set on a previous instruc- tion. both wr and wren cannot be set with the same instruction. at the completion of the write cycle, the wr bit is cleared in hardware and the eeprom write complete interrupt flag bit (eeif) is set. the user may either enable this interrupt, or poll this bit. eeif must be cleared by software. example 6-2: data eeprom write movlw data_ee_addr ; movwf eeadr ; data memory address to read bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access program flash or data eeprom memory bsf eecon1, rd ; eeprom read movf eedata, w ; w = eedata movlw data_ee_addr ; movwf eeadr ; data memory address to read movlw data_ee_data ; movwf eedata ; data memory value to write bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access program flash or data eeprom memory bsf eecon1, wren ; enable writes bcf intcon, gie ; disable interrupts required movlw 55h ; sequence movwf eecon2 ; write 55h movlw aah ; movwf eecon2 ; write aah bsf eecon1, wr ; set wr bit to begin write bsf intcon, gie ; enable interrupts . ; user code execution . . bcf eecon1, wren ; disable writes on write complete (eeif set)
pic18fxx39 ds30485a-page 64 preliminary ? 2002 microchip technology inc. 6.5 write verify depending on the application, good programming practice may dictate that the value written to the mem- ory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. 6.6 protection against spurious write there are conditions when the device may not want to write to the data eeprom memory. to protect against spurious eeprom writes, various mechanisms have been built-in. on power-up, the wren bit is cleared. also, the power-up timer (72 ms duration) prevents eeprom write. the write initiate sequence and the wren bit together help prevent an accidental write during brown-out, power glitch, or software malfunction. 6.7 operation during code protect data eeprom memory has its own code protect mechanism. external read and write operations are disabled if either of these mechanisms are enabled. the microcontroller itself can both read and write to the internal data eeprom, regardless of the state of the code protect configuration bit. refer to ?special features of the cpu? (section 20.0) for additional information. 6.8 using the data eeprom the data eeprom is a high endurance, byte address- able array that has been optimized for the storage of frequently changing information (e.g., program vari- ables or other data that are updated often). frequently changing values will typically be updated more often than specification d124. if this is not the case, an array refresh must be performed. for this reason, variables that change infrequently (such as constants, ids, cali- bration, etc.) should be stored in flash program memory. a simple data eeprom refresh routine is shown in example 6-3. example 6-3: data eeprom refresh routine note: if data eeprom is only used to store con- stants and/or data that changes rarely, an array refresh is likely not required. see specification d124. clrf eeadr ; start at address 0 bcf eecon1,cfgs ; set for memory bcf eecon1,eepgd ; set for data eeprom bcf intcon,gie ; disable interrupts bsf eecon1,wren ; enable writes loop ; loop to refresh array bsf eecon1,rd ; read current address movlw 55h ; movwf eecon2 ; write 55h movlw aah ; movwf eecon2 ; write aah bsf eecon1,wr ; set wr bit to begin write btfsc eecon1,wr ; wait for write to complete bra $-2 incfsz eeadr,f ; increment address bra loop ; not zero, do it again bcf eecon1,wren ; disable writes bsf intcon,gie ; enable interrupts
? 2002 microchip technology inc. preliminary ds30485a-page 65 pic18fxx39 table 6-1: registers associated with data eeprom memory address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets ff2h intcon gie/ gieh peie/ giel t0ie inte rbie t0if intf rbif 0000 000x 0000 000u fa9h eeadr eeprom address register 0000 0000 0000 0000 fa8h eedata eeprom data register 0000 0000 0000 0000 fa7h eecon2 eeprom control register2 (not a physical register) ? ? fa6h eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 uu-0 u000 fa2h ipr2 ? ? ? eeip bclip lvdip tmr3ip ? ---1 1111 ---1 1111 fa1h pir2 ? ? ?eeif bclif lvdif tmr3if ? ---0 0000 ---0 0000 fa0h pie2 ? ? ? eeie bclie lvdie tmr3ie ? ---0 0000 ---0 0000 legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used during flash/eeprom access.
pic18fxx39 ds30485a-page 66 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 67 pic18fxx39 7.0 8 x 8 hardware multiplier 7.1 introduction an 8 x 8 hardware multiplier is included in the alu of the pic18fxx39 devices. by making the multiply a hardware operation, it completes in a single instruction cycle. this is an unsigned multiply that gives a 16-bit result. the result is stored into the 16-bit product regis- ter pair (prodh:prodl). the multiplier does not affect any flags in the alusta register. making the 8 x 8 multiplier execute in a single cycle gives the following advantages: ? higher computational throughput ? reduces code size requirements for multiply algorithms the performance increase allows the device to be used in applications previously reserved for digital signal processors. table 7-1 shows a performance comparison between enhanced devices using the single cycle hardware mul- tiply, and performing the same function without the hardware multiply. 7.2 operation example 7-1 shows the sequence to do an 8 x 8 unsigned multiply. only one instruction is required when one argument of the multiply is already loaded in the wreg register. example 7-2 shows the sequence to do an 8 x 8 signed multiply. to account for the sign bits of the arguments, each argument?s most significant bit (msb) is tested and the appropriate subtractions are done. example 7-1: 8 x 8 unsigned multiply routine example 7-2: 8 x 8 signed multiply routine table 7-1: performance comparison movf arg1, w ; mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl movf arg1, w mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl btfsc arg2, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg1 movf arg2, w btfsc arg1, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg2 routine multiply method program memory (words) cycles (max) time @ 40 mhz @ 10 mhz @ 4 mhz 8 x 8 unsigned without hardware multiply 13 69 6.9 s27.6 s69 s hardware multiply 1 1 100 ns 400 ns 1 s 8 x 8 signed without hardware multiply 33 91 9.1 s36.4 s91 s hardware multiply 6 6 600 ns 2.4 s6 s 16 x 16 unsigned without hardware multiply 21 242 24.2 s96.8 s 242 s hardware multiply 28 28 2.8 s 11.2 s28 s 16 x 16 signed without hardware multiply 52 254 25.4 s 102.6 s 254 s hardware multiply 35 40 4.0 s16.0 s40 s
pic18fxx39 ds30485a-page 68 preliminary ? 2002 microchip technology inc. example 7-3 shows the sequence to do a 16 x 16 unsigned multiply. equation 7-1 shows the algorithm that is used. the 32-bit result is stored in four registers, res3:res0. equation 7-1: 16 x 16 unsigned multiplication algorithm example 7-3: 16 x 16 unsigned multiply routine example 7-4 shows the sequence to do a 16 x 16 signed multiply. equation 7-2 shows the algorithm used. the 32-bit result is stored in four registers, res3:res0. to account for the sign bits of the argu- ments, each argument pairs most significant bit (msb) is tested and the appropriate subtractions are done. equation 7-2: 16 x 16 signed multiplication algorithm example 7-4: 16 x 16 signed multiply routine res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) movf arg1l, w mulwf arg2l ; arg1l * arg2l -> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h -> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; res3:res0 =arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) + (-1 ? arg2h<7> ? arg1h:arg1l ? 2 16 ) + (-1 ? arg1h<7> ? arg2h:arg2l ? 2 16 ) movf arg1l, w mulwf arg2l ; arg1l * arg2l -> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h -> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; btfss arg2h, 7 ; arg2h:arg2l neg? bra sign_arg1 ; no, check arg1 movf arg1l, w ; subwf res2 ; movf arg1h, w ; subwfb res3 ; sign_arg1 btfss arg1h, 7 ; arg1h:arg1l neg? bra cont_code ; no, done movf arg2l, w ; subwf res2 ; movf arg2h, w ; subwfb res3 ; cont_code :
? 2002 microchip technology inc. preliminary ds30485a-page 69 pic18fxx39 8.0 interrupts the pic18fxx39 devices have multiple interrupt sources and an interrupt priority feature that allows each interrupt source to be assigned a high priority level or a low priority level. the high priority interrupt vector is at 000008h and the low priority interrupt vector is at 000018h. high priority interrupt events will override any low priority interrupts that may be in progress. there are ten registers which are used to control interrupt operation. these registers are: ? rcon ?intcon ? intcon2 ? intcon3 ? pir1, pir2 ? pie1, pie2 ? ipr1, ipr2 it is recommended that the microchip header files sup- plied with mplab ? ide be used for the symbolic bit names in these registers. this allows the assembler/ compiler to automatically take care of the placement of these bits within the specified register. each interrupt source, except int0, has three bits to control its operation. the functions of these bits are: ? flag bit to indicate that an interrupt event occurred ? enable bit that allows program execution to branch to the interrupt vector address when the flag bit is set ? priority bit to select high priority or low priority the interrupt priority feature is enabled by setting the ipen bit (rcon<7>). when interrupt priority is enabled, there are two bits which enable interrupts glo- bally. setting the gieh bit (intcon<7>) enables all interrupts that have the priority bit set. setting the giel bit (intcon<6>) enables all interrupts that have the priority bit cleared. when the interrupt flag, enable bit and appropriate global interrupt enable bit are set, the interrupt will vector immediately to address 000008h or 000018h, depending on the priority level. individual interrupts can be disabled through their corresponding enable bits. while pic18fxx39 devices have two interrupt priority levels like other pic18 microcontrollers, their allocation is different. in these devices, the high priority interrupt is used exclusively by the prompt kernel via the timer2 match interrupt. in order for the kernel to func- tion properly, it is imperative that all other interrupts either set as low priority (ipr bit = 0), or disabled. when an interrupt is responded to, the global interrupt enable bit is cleared to disable further interrupts. if the ipen bit is cleared, this is the gie bit. if interrupt priority levels are used, this will be either the gieh or giel bit. high priority interrupt sources can interrupt a low priority interrupt. the return address is pushed onto the stack and the pc is loaded with the interrupt vector address (000008h or 000018h). once in the interrupt service routine, the source(s) of the interrupt can be deter- mined by polling the interrupt flag bits. the interrupt flag bits must be cleared in software before re-enabling interrupts to avoid recursive interrupts. the ?return from interrupt? instruction, retfie , exits the interrupt routine and sets the gieh or giel bits (as applicable), which re-enables interrupts. for external interrupt events, such as the int pins or the portb input change interrupt, the interrupt latency will be three to four instruction cycles. the exact latency is the same for one or two-cycle instructions. individual interrupt flag bits are set, regardless of the status of their corresponding enable bit or the gie bit. note: disabling interrupts, or setting interrupts as low priority, is not the same as disabling interrupt priorities. the interrupt priority levels must remain enabled (ipen = 1). clearing the ipen bit will result in erratic operation of the prompt kernel. note: do not use the movff instruction to modify any of the interrupt control registers while any interrupt is enabled. doing so may cause erratic microcontroller behavior.
pic18fxx39 ds30485a-page 70 preliminary ? 2002 microchip technology inc. figure 8-1: interrupt logic tmr0ie gieh/gie wake-up if in sleep mode interrupt to cpu vector to location 0008h int2if int2ie int2ip int1if int1ie int1ip tmr0if tmr0ie tmr0ip rbif rbie rbip tmr0if tmr0ip int1if int1ie int1ip int2if int2ie int2ip rbif rbie rbip int0if int0ie giel/peie interrupt to cpu vector to location 0018h peripheral interrupt flag bit peripheral interrupt enable bit peripheral interrupt priority bit peripheral interrupt flag bit peripheral interrupt enable bit peripheral interrupt priority bit tmr2if tmr2ie tmr2ip xxxxif xxxxie xxxxip additional peripheral interrupts tmr1if tmr1ie tmr1ip high priority interrupt generation low priority interrupt generation xxxxif xxxxie xxxxip additional peripheral interrupts gie/gieh
? 2002 microchip technology inc. preliminary ds30485a-page 71 pic18fxx39 8.1 intcon registers the intcon registers are readable and writable registers, which contain various enable, priority and flag bits. register 8-1: intcon register note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-x gie/gieh peie/giel tmr0ie int0ie (1) rbie tmr0if int0if rbif (2) bit 7 bit 0 bit 7 gie/gieh: global interrupt enable bit 1 = enables all high priority interrupts 0 = disables all interrupts bit 6 peie/giel: peripheral interrupt enable bit 1 = enables all low priority peripheral interrupts 0 = disables all low priority peripheral interrupts bit 5 tmr0ie: tmr0 overflow interrupt enable bit 1 = enables the tmr0 overflow interrupt 0 = disables the tmr0 overflow interrupt bit 4 int0ie (1) : int0 external interrupt enable bit 1 = enables the int0 external interrupt 0 = disables the int0 external interrupt bit 3 rbie: rb port change interrupt enable bit 1 = enables the rb port change interrupt 0 = disables the rb port change interrupt bit 2 tmr0if: tmr0 overflow interrupt flag bit 1 = tmr0 register has overflowed (must be cleared in software) 0 = tmr0 register did not overflow bit 1 int0if: int0 external interrupt flag bit 1 = the int0 external interrupt occurred (must be cleared in software) 0 = the int0 external interrupt did not occur bit 0 rbif (2) : rb port change interrupt flag bit 1 = at least one of the rb7:rb4 pins changed state (must be cleared in software) 0 = none of the rb7:rb4 pins have changed state note 1: maintain this bit cleared (= 0). 2: a mismatch condition will continue to set this bit. reading portb will end the mismatch condition and allow the bit to be cleared. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 72 preliminary ? 2002 microchip technology inc. register 8-2: intcon2 register r/w-1 r/w-1 r/w-1 r/w-1 u-0 r/w-1 u-0 r/w-1 rbpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip (1) bit 7 bit 0 bit 7 rbpu : portb pull-up enable bit 1 = all portb pull-ups are disabled 0 = portb pull-ups are enabled by individual port latch values bit 6 intedg0 : external interrupt 0 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 5 intedg1 : external interrupt 1 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 4 intedg2 : external interrupt 2 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 3 unimplemented: read as '0' bit 2 tmr0ip (1) : tmr0 overflow interrupt priority bit 1 = high priority 0 = low priority bit 1 unimplemented: read as '0' bit 0 rbip (1) : rb port change interrupt priority bit 1 = high priority 0 = low priority note 1: maintain this bit cleared (= 0). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling.
? 2002 microchip technology inc. preliminary ds30485a-page 73 pic18fxx39 register 8-3: intcon3 register r/w-1 r/w-1 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 int2ip (1) int1ip (1) ?int2ieint1ie ? int2if int1if bit 7 bit 0 bit 7 int2ip (1) : int2 external interrupt priority bit 1 = high priority 0 = low priority bit 6 int1ip (1) : int1 external interrupt priority bit 1 = high priority 0 = low priority bit 5 unimplemented: read as '0' bit 4 int2ie: int2 external interrupt enable bit 1 = enables the int2 external interrupt 0 = disables the int2 external interrupt bit 3 int1ie: int1 external interrupt enable bit 1 = enables the int1 external interrupt 0 = disables the int1 external interrupt bit 2 unimplemented: read as '0' bit 1 int2if: int2 external interrupt flag bit 1 = the int2 external interrupt occurred (must be cleared in software) 0 = the int2 external interrupt did not occur bit 0 int1if: int1 external interrupt flag bit 1 = the int1 external interrupt occurred (must be cleared in software) 0 = the int1 external interrupt did not occur note 1: maintain this bit cleared (= 0). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling.
pic18fxx39 ds30485a-page 74 preliminary ? 2002 microchip technology inc. 8.2 pir registers the pir registers contain the individual flag bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt flag registers (pir1, pir2). register 8-4: pir1: peripheral interrupt request (flag) register 1 note 1: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit, gie (intcon<7>). 2: user software should ensure the appropri- ate interrupt flag bits are cleared prior to enabling an interrupt, and after servicing that interrupt. r/w-0 r/w-0 r-0 r-0 r/w-0 u-0 r/w-0 r/w-0 pspif (1) adif rcif txif sspif ?tmr2if (2) tmr1if bit 7 bit 0 bit 7 pspif (1) : parallel slave port read/write interrupt flag bit 1 = a read or a write operation has taken place (must be cleared in software) 0 = no read or write has occurred bit 6 adif : a/d converter interrupt flag bit 1 = an a/d conversion completed (must be cleared in software) 0 = the a/d conversion is not complete bit 5 rcif : usart receive interrupt flag bit 1 = the usart receive buffer, rcreg, is full (cleared when rcreg is read) 0 = the usart receive buffer is empty bit 4 txif : usart transmit interrupt flag bit (see section 17.0 for details on txif functionality) 1 = the usart transmit buffer, txreg, is empty (cleared when txreg is written) 0 = the usart transmit buffer is full bit 3 sspif : master synchronous serial port interrupt flag bit 1 = the transmission/reception is complete (must be cleared in software) 0 = waiting to transmit/receive bit 2 unimplemented: read as ?0? bit 1 tmr2if (2) : tmr2 to pr2 match interrupt flag bit 1 = tmr2 to pr2 match occurred (must be cleared in software) 0 = no tmr2 to pr2 match occurred bit 0 tmr1if: tmr1 overflow interrupt flag bit 1 = tmr1 register overflowed (must be cleared in software) 0 = mr1 register did not overflow note 1: this bit is reserved on pic18f2x39 devices; always maintain this bit clear. 2: this bit is reserved for use by the prompt kernel; do not alter its value. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 75 pic18fxx39 register 8-5: pir2: peripheral interrupt request (flag) register 2 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 ? ? ? eeif bclif lvdif tmr3if ? bit 7 bit 0 bit 7-5 unimplemented: read as '0' bit 4 eeif : data eeprom/flash write operation interrupt flag bit 1 = the write operation is complete (must be cleared in software) 0 = the write operation is not complete, or has not been started bit 3 bclif : bus collision interrupt flag bit 1 = a bus collision occurred (must be cleared in software) 0 = no bus collision occurred bit 2 lvdif : low voltage detect interrupt flag bit 1 = a low voltage condition occurred (must be cleared in software) 0 = the device voltage is above the low voltage detect trip point bit 1 tmr3if : tmr3 overflow interrupt flag bit 1 = tmr3 register overflowed (must be cleared in software) 0 = tmr3 register did not overflow bit 0 unimplemented: read as ?0? legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 76 preliminary ? 2002 microchip technology inc. 8.3 pie registers the pie registers contain the individual enable bits for the peripheral interrupts. due to the number of periph- eral interrupt sources, there are two peripheral inter- rupt enable registers (pie1, pie2). when ipen = 0, the peie bit must be set to enable any of these peripheral interrupts. register 8-6: pie1: peripheral interrupt enable register 1 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 pspie (1) adie rcie txie sspie ?tmr2ie (2) tmr1ie bit 7 bit 0 bit 7 pspie (1) : parallel slave port read/write interrupt enable bit 1 = enables the psp read/write interrupt 0 = disables the psp read/write interrupt bit 6 adie : a/d converter interrupt enable bit 1 = enables the a/d interrupt 0 = disables the a/d interrupt bit 5 rcie : usart receive interrupt enable bit 1 = enables the usart receive interrupt 0 = disables the usart receive interrupt bit 4 txie : usart transmit interrupt enable bit 1 = enables the usart transmit interrupt 0 = disables the usart transmit interrupt bit 3 sspie : master synchronous serial port interrupt enable bit 1 = enables the mssp interrupt 0 = disables the mssp interrupt bit 2 unimplemented: read as ?0? bit 1 tmr2ie (2) : tmr2 to pr2 match interrupt enable bit 1 = enables the tmr2 to pr2 match interrupt 0 = disables the tmr2 to pr2 match interrupt bit 0 tmr1ie : tmr1 overflow interrupt enable bit 1 = enables the tmr1 overflow interrupt 0 = disables the tmr1 overflow interrupt note 1: this bit is reserved on pic18f2x39 devices; always maintain this bit clear. 2: this bit is reserved for use by the prompt kernel; do not alter its value. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 77 pic18fxx39 register 8-7: pie2: peripheral interrupt enable register 2 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 ? ? ? eeie bclie lvdie tmr3ie ? bit 7 bit 0 bit 7-5 unimplemented: read as '0' bit 4 eeie : data eeprom/flash write operation interrupt enable bit 1 = enabled 0 = disabled bit 3 bclie : bus collision interrupt enable bit 1 = enabled 0 = disabled bit 2 lvdie : low voltage detect interrupt enable bit 1 = enabled 0 = disabled bit 1 tmr3ie : tmr3 overflow interrupt enable bit 1 = enables the tmr3 overflow interrupt 0 = disables the tmr3 overflow interrupt bit 0 unimplemented: read as ?0? legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 78 preliminary ? 2002 microchip technology inc. 8.4 ipr registers the ipr registers contain the individual priority bits for the peripheral interrupts. due to the number of periph- eral interrupt sources, there are two peripheral inter- rupt priority registers (ipr1, ipr2). the operation of the priority bits requires that the interrupt priority enable (ipen) bit be set. for pic18fxx39 devices, the motor control kernel requires that the timer2 to pr2 match interrupt be the only high priority interrupt. failure to do this may result in unpredictable operation of the kernel or the entire microcontroller. in practical terms, this means: ? interrupt priority levels are enabled (ipen = 1); ? high priority interrupts are enabled (intcon<7> = 1); ? timer2 interrupt is enabled and set as high priority (pie1<1> and ipr<1> = 1); and ? all other interrupts are disabled (intcon or pir bits = 0), or set as low priority (ipr bits = 0). register 8-8: ipr1: peripheral interrupt priority register 1 note: configuring the interrupts is automatically done by the api method void prompt_init(pwmfrequency ). it is the user?s responsibility to make certain that this method is called at the very beginning of the application. r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 u-1 r/w-1 r/w-1 pspip (1,2) adip (2) rcip (2) txip (2) sspip (2) ?tmr2ip (3) tmr1ip (2) bit 7 bit 0 bit 7 pspip (1,2) : parallel slave port read/write interrupt priority bit 1 = high priority 0 = low priority bit 6 adip (2) : a/d converter interrupt priority bit 1 = high priority 0 = low priority bit 5 rcip (2) : usart receive interrupt priority bit 1 = high priority 0 = low priority bit 4 txip (2) : usart transmit interrupt priority bit 1 = high priority 0 = low priority bit 3 sspip (2) : master synchronous serial port interrupt priority bit 1 = high priority 0 = low priority bit 2 unimplemented: read as ?1? bit 1 tmr2ip (3) : tmr2 to pr2 match interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr1ip (2) : tmr1 overflow interrupt priority bit 1 = high priority 0 = low priority note 1: this bit is reserved on pic18f2x39 devices. 2: maintain this bit cleared (= 0). 3: this bit is reserved for use by the prompt kernel; always maintain this bit set (= 1). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 79 pic18fxx39 register 8-9: ipr2: peripheral interrupt priority register 2 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 u-1 ? ? ? eeip (1) bclip (1) lvdip (1) tmr3ip (1) ? bit 7 bit 0 bit 7-5 unimplemented: read as '0' bit 4 eeip (1) : data eeprom/flash write operation interrupt priority bit 1 = high priority 0 = low priority bit 3 bclip (1) : bus collision interrupt priority bit 1 = high priority 0 = low priority bit 2 lvdip (1) : low voltage detect interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr3ip (1) : tmr3 overflow interrupt priority bit 1 = high priority 0 = low priority bit 0 unimplemented: read as ?1? note 1: maintain this bit cleared (= 0). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 80 preliminary ? 2002 microchip technology inc. 8.5 rcon register the rcon register contains the bit which is used to enable prioritized interrupts (ipen). for pic18fxx39 devices, the ipen bit must always be set (= 1) for the prompt kernel to function correctly. refer to page 69 for a more detailed discussion on interrupt priorities. register 8-10: rcon register r/w-0 u-0 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen (1) ? ?ri to pd por bor bit 7 bit 0 bit 7 ipen (1) : interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (not used) bit 6-5 unimplemented: read as '0' bit 4 ri : reset instruction flag bit for details of bit operation, see register 4-3 bit 3 to : watchdog time-out flag bit for details of bit operation, see register 4-3 bit 2 pd : power-down detection flag bit for details of bit operation, see register 4-3 bit 1 por : power-on reset status bit for details of bit operation, see register 4-3 bit 0 bor : brown-out reset status bit for details of bit operation, see register 4-3 note 1: maintain this bit set (= 1). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 81 pic18fxx39 8.6 int0 interrupt external interrupts on the rb0/int0, rb1/int1 and rb2/int2 pins are edge triggered: either rising, if the corresponding intedgx bit is set in the intcon2 reg- ister, or falling, if the intedgx bit is clear. when a valid edge appears on the rbx/intx pin, the corresponding flag bit intxf is set. this interrupt can be disabled by clearing the corresponding enable bit intxe. flag bit intxf must be cleared in software in the interrupt ser- vice routine before re-enabling the interrupt. all exter- nal interrupts (int0, int1 and int2) can wake-up the processor from sleep, if bit intxe was set prior to going into sleep. if the global interrupt enable bit gie is set, the processor will branch to the interrupt vector following wake-up. the int0 interrupt is always configured as a high prior- ity interrupt, and cannot be reconfigured. interrupt pri- ority for int1 and int2 is determined by the value contained in the interrupt priority bits, int1ip (intcon3<6>) and int2ip (intcon3<7>). because it is always configured as a high priority inter- rupt, int0 cannot be used in conjunction with the prompt kernel; it must always be disabled (intcon<4> = 0). failure to do this may result in erratic operation of the motor control. 8.7 tmr0 interrupt in 8-bit mode (which is the default), an overflow in the tmr0 register (ffh 00h) will set flag bit tmr0if. in 16-bit mode, an overflow in the tmr0h:tmr0l regis- ter pair (ffffh 0000h) will set flag bit tmr0if. the interrupt can be enabled or disabled by setting or clearing enable bit tmr0ie (intcon<5>). interrupt priority for timer0 is determined by the value contained in the interrupt priority bit tmr0ip (intcon2<2>). see section 10.0 for further details on the timer0 module. 8.8 portb interrupt-on-change an input change on portb<7:4> sets flag bit rbif (intcon<0>). the interrupt can be enabled or dis- abled by setting or clearing the enable bit rbie (intcon<3>). interrupt priority for portb interrupt- on-change is determined by the value contained in the interrupt priority bit rbip (intcon2<0>). 8.9 context saving during interrupts during an interrupt, the return pc value is saved on the stack. additionally, the wreg, status and bsr regis- ters are saved on the fast return stack. if a fast return from interrupt is not used (see section 4.3), the user may need to save the wreg, status and bsr regis- ters in software. depending on the user?s application, other registers may also need to be saved. example 8-1 saves and restores the wreg, status and bsr registers during an interrupt service routine. example 8-1: saving status, wreg and bsr registers in ram movwf w_temp ; w_temp is in virtual bank movff status, status_temp ; status_temp located anywhere movff bsr, bsr_temp ; bsr located anywhere ; ; user isr code ; movff bsr_temp, bsr ; restore bsr movf w_temp, w ; restore wreg movff status_temp,status ; restore status
pic18fxx39 ds30485a-page 82 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 83 pic18fxx39 9.0 i/o ports depending on the device selected, there are either three or five ports available. some pins of the i/o ports are multiplexed with an alternate function from the peripheral features on the device. in general, when a peripheral is enabled, that pin may not be used as a general purpose i/o pin. each port has three registers for its operation. these registers are: ? tris register (data direction register) ? port register (reads the levels on the pins of the device) ? lat register (output latch) the data latch (lat register) is useful for read-modify- write operations on the value that the i/o pins are driving. 9.1 porta, trisa and lata registers porta is a 7-bit wide, bi-directional port. the corre- sponding data direction register is trisa. setting a trisa bit (= 1) will make the corresponding porta pin an input (i.e., put the corresponding output driver in a high impedance mode). clearing a trisa bit (= 0) will make the corresponding porta pin an output (i.e., put the contents of the output latch on the selected pin). reading the porta register reads the status of the pins, whereas writing to it will write to the port latch. the data latch register (lata) is also memory mapped. read-modify-write operations on the lata register reads and writes the latched output value for porta. the ra4 pin is multiplexed with the timer0 module clock input to become the ra4/t0cki pin. the ra4/ t0cki pin is a schmitt trigger input and an open drain output. all other ra port pins have ttl input levels and full cmos output drivers. the other porta pins are multiplexed with analog inputs and the analog v ref + and v ref - inputs. the operation of each pin is selected by clearing/setting the control bits in the adcon1 register (a/d control register1). the trisa register controls the direction of the ra pins, even when they are being used as analog inputs. the user must ensure the bits in the trisa register are maintained set when using them as analog inputs. example 9-1: initializing porta figure 9-1: block diagram of ra3:ra0 and ra5 pins note: on a power-on reset, ra5 and ra3:ra0 are configured as analog inputs and read as ?0?. ra6 and ra4 are configured as digital inputs. clrf porta ; initialize porta by ; clearing output ; data latches clrf lata ; alternate method ; to clear output ; data latches movlw 0x07 ; configure a/d movwf adcon1 ; for digital inputs movlw 0xcf ; value used to ; initialize data ; direction movwf trisa ; set ra<3:0> as inputs ; ra<5:4> as outputs data bus q d q ck q d q ck qd en p n wr lata wr trisa data latch tris latch rd trisa rd porta v ss v dd i/o pin (1) note 1: i/o pins have protection diodes to v dd and v ss . analog input mode ttl input buffer to a/d converter and lvd modules rd lata or porta ss input (ra5 only)
pic18fxx39 ds30485a-page 84 preliminary ? 2002 microchip technology inc. figure 9-2: block diagram of ra4/t0cki pin figure 9-3: block diagram of ra6 pin data bus wr trisa rd porta data latch tris latch rd trisa schmitt trigger input buffer n v ss i/o pin (1) tmr0 clock input q d q ck q d q ck en qd en rd lata wr lata or porta note 1: i/o pin has protection diode to v ss only. data bus q d q ck qd en p n wr lata wr data latch tris latch rd trisa rd porta v ss v dd i/o pin (1) note 1: i/o pins have protection diodes to v dd and v ss . or porta rd lata ecra6 or ecra6 or enable ttl input buffer rcra6 rcra6 enable trisa q d q ck
? 2002 microchip technology inc. preliminary ds30485a-page 85 pic18fxx39 table 9-1: porta functions table 9-2: summary of registers associated with porta name bit# buffer function ra0/an0 bit0 ttl input/output or analog input. ra1/an1 bit1 ttl input/output or analog input. ra2/an2/v ref - bit2 ttl input/output or analog input or v ref -. ra3/an3/v ref + bit3 ttl input/output or analog input or v ref +. ra4/t0cki bit4 st input/output or external clock input for timer0. output is open drain type. ra5/ss /an4/lvdin bit5 ttl input/output or slave select input for synchronous serial port or analog input, or low voltage detect input. osc2/clko/ra6 bit6 ttl osc2 or clock output or i/o pin. legend: ttl = ttl input, st = schmitt trigger input name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets porta ? ra6 ra5 ra4 ra3 ra2 ra1 ra0 -x0x 0000 -u0u 0000 lata ? lata data output register -xxx xxxx -uuu uuuu trisa ? porta data direction register -111 1111 -111 1111 adcon1 adfm adcs2 ? ? pcfg3 pcfg2 pcfg1 pcfg0 00-- 0000 00-- 0000 legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. shaded cells are not used by porta.
pic18fxx39 ds30485a-page 86 preliminary ? 2002 microchip technology inc. 9.2 portb, trisb and latb registers portb is an 8-bit wide, bi-directional port. the corre- sponding data direction register is trisb. setting a trisb bit (= 1) will make the corresponding portb pin an input (i.e., put the corresponding output driver in a high impedance mode). clearing a trisb bit (= 0) will make the corresponding portb pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (latb) is also memory mapped. read-modify-write operations on the latb register reads and writes the latched output value for portb. example 9-2: initializing portb each of the portb pins has a weak internal pull-up. a single control bit can turn on all the pull-ups. this is per- formed by clearing bit rbpu (intcon2<7>). the weak pull-up is automatically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. four of the portb pins, rb7:rb4, have an interrupt- on-change feature. only pins configured as inputs can cause this interrupt to occur (i.e., any rb7:rb4 pin configured as an output is excluded from the interrupt- on-change comparison). the input pins (of rb7:rb4) are compared with the old value latched on the last read of portb. the ?mismatch? outputs of rb7:rb4 are or?ed together to generate the rb port change interrupt with flag bit, rbif (intcon<0>). this interrupt can wake the device from sleep. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of portb (except with the movff instruction). this will end the mismatch condition. b) clear flag bit rbif. a mismatch condition will continue to set flag bit rbif. reading portb will end the mismatch condition and allow flag bit rbif to be cleared. the interrupt-on-change feature is recommended for wake-up on key depression operation and operations where portb is only used for the interrupt-on-change feature. polling of portb is not recommended while using the interrupt-on-change feature. figure 9-4: block diagram of rb7:rb4 pins note: on a power-on reset, these pins are configured as digital inputs. clrf portb ; initialize portb by ; clearing output ; data latches clrf latb ; alternate method ; to clear output ; data latches movlw 0xcf ; value used to ; initialize data ; direction movwf trisb ; set rb<3:0> as inputs ; rb<5:4> as outputs ; rb<7:6> as inputs note 1: while in low voltage icsp mode, the rb5 pin can no longer be used as a gen- eral purpose i/o pin, and should be held low during normal operation to protect against inadvertent icsp mode entry. 2: when using low voltage icsp program- ming (lvp), the pull-up on rb5 becomes disabled. if trisb bit 5 is cleared, thereby setting rb5 as an output, latb bit 5 must also be cleared for proper operation. data latch from other rbpu (2) p v dd i/o pin (1) q d ck q d ck qd en qd en data bus wr latb wr trisb set rbif tris latch rd trisb rd portb rb7:rb4 pins weak pull-up rd portb latch ttl input buffer st buffer rb7:rb5 in serial programming mode q3 q1 rd latb or portb note 1: i/o pins have diode protection to v dd and v ss . 2: to enable weak pull-ups, set the appropriate tris bit(s) and clear the rbpu bit (intcon2<7>).
? 2002 microchip technology inc. preliminary ds30485a-page 87 pic18fxx39 figure 9-5: block diagram of rb2:rb0 pins figure 9-6: block diagram of rb3 pin data latch rbpu (2) p v dd q d ck q d ck qd en data bus wr port wr tris rd tris rd port weak pull-up rb0/int i/o pin (1) ttl input buffer schmitt trigger buffer tris latch note 1: i/o pins have diode protection to v dd and v ss . 2: to enable weak pull-ups, set the appropriate tris bit(s) and clear the rbpu bit (option_reg<7>). data latch p v dd q d ck q d en data bus wr latb or wr trisb rd trisb weak pull-up ttl input buffer tris latch rd latb wr portb rbpu (2) ck d rd portb p n v dd v ss i/o pin (1) q ?1? note 1: i/o pin has diode protection to v dd and v ss . 2: to enable weak pull-ups, set the appropriate ddr bit(s) and clear the rbpu bit (intcon2<7>).
pic18fxx39 ds30485a-page 88 preliminary ? 2002 microchip technology inc. table 9-3: portb functions table 9-4: summary of registers associated with portb name bit# buffer function rb0/int0 bit0 ttl/st (1) input/output pin or external interrupt input0. internal software programmable weak pull-up. rb1/int1 bit1 ttl/st (1) input/output pin or external interrupt input1. internal software programmable weak pull-up. rb2/int2 bit2 ttl/st (1) input/output pin or external interrupt input2. internal software programmable weak pull-up. rb3 bit3 ttl input/output pin. internal software programmable weak pull-up. rb4 bit4 ttl input/output pin (with interrupt-on-change). internal software programmable weak pull-up. rb5/pgm (4) bit5 ttl/st (2) input/output pin (with interrupt-on-change). internal software programmable weak pull-up. low voltage icsp enable pin. rb6/pgc bit6 ttl/st (2) input/output pin (with interrupt-on-change). internal software programmable weak pull-up. serial programming clock. rb7/pgd bit7 ttl/st (2) input/output pin (with interrupt-on-change). internal software programmable weak pull-up. serial programming data. legend: ttl = ttl input, st = schmitt trigger input note 1: this buffer is a schmitt trigger input when configured as the external interrupt. 2: this buffer is a schmitt trigger input when used in serial programming mode. 3: a device configuration bit selects which i/o pin the ccp2 pin is multiplexed on. 4: low voltage icsp programming (lvp) is enabled by default, which disables the rb5 i/o function. lvp must be disabled to enable rb5 as an i/o pin and allow maximum compatibility to the other 28-pin and 40-pin mid-range devices. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portb rb7 rb6 rb5 rb4 rb3 rb2 rb1 rb0 xxxx xxxx uuuu uuuu latb latb data output register xxxx xxxx uuuu uuuu trisb portb data direction register 1111 1111 1111 1111 intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u intcon2 rbpu intedg0 intedg1 intedg2 ? tmr0ip ?rbip 1111 -1-1 1111 -1-1 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 11-0 0-00 11-0 0-00 legend: x = unknown, u = unchanged. shaded cells are not used by portb.
? 2002 microchip technology inc. preliminary ds30485a-page 89 pic18fxx39 9.3 portc, trisc and latc registers portc is a 6-bit wide, bi-directional port. the corre- sponding data direction register is trisc. setting a trisc bit (= 1) will make the corresponding portc pin an input (i.e., put the corresponding output driver in a high impedance mode). clearing a trisc bit (= 0) will make the corresponding portc pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (latc) is also memory mapped. read-modify-write operations on the latc register reads and writes the latched output value for portc. portc is multiplexed with the serial communication functions (table 9-5). portc pins have schmitt trigger input buffers. when enabling peripheral functions, care should be taken in defining tris bits for each portc pin. some peripherals override the tris bit to make a pin an out- put, while other peripherals override the tris bit to make a pin an input. the user should refer to the corre- sponding peripheral section for the correct tris bit settings. the pin override value is not loaded into the tris reg- ister. this allows read-modify-write of the tris register, without concern due to peripheral overrides. example 9-3: initializing portc pic18fxx39 devices differ from other pic18 micro- controllers in allocation of portc pins. for most pic18 devices, portc is an 8-bit-wide port. for the pic18fxx39 family, two of the portc pins (rc1 and rc2) are re-allocated as pwm output only pins for use with the motor control kernel. to maintain pinout com- patibility with other picmicro ? devices, the remaining portc pins are assigned in a manner consistent with other pic18 devices. for this reason, portc has pins rc0 and rc3 through rc7, but not rc1 and rc2. to maintain compatibility with pic18fxx2 devices, the individual port and corresponding latch and direction bits for rc1 and rc2 are present in the appropriate registers, but are not available to the user. to avoid erratic device operation, the values of these bits should not be modified. figure 9-7: portc block diagram (peripheral output override) note: on a power-on reset, these pins are configured as digital inputs. clrf portc ; initialize portc by ; clearing output ; data latches clrf latc ; alternate method ; to clear output ; data latches movlw 0xc 9 ; value used to ; initialize data ; direction movwf trisc ; set rc<3>,rc<0> as inputs, ; rc<5:4> as outputs, and ; rc<7:6> as inputs data bus wr latc or wr trisc rd trisc q d q ck q d en peripheral data out 0 1 q d q ck rd portc peripheral data in wr portc rd latc peripheral output schmitt port/peripheral select (2) enable (3) p n v ss v dd i/o pin (1) note 1: i/o pins have diode protection to v dd and v ss . 2: port/peripheral select signal selects between port data (input) and peripheral output. 3: peripheral output enable is only active if peripheral select is active. data latch tris latch trigger
pic18fxx39 ds30485a-page 90 preliminary ? 2002 microchip technology inc. table 9-5: portc functions table 9-6: summary of registers associated with portc name bit# buffer type function rc0/t13cki bit0 st input/output port pin or timer1 oscillator output/timer1 clock input. rc3/sck/scl bit3 st rc3 can also be the synchronous serial clock for both spi and i 2 c modes. rc4/sdi/sda bit4 st rc4 can also be the spi data in (spi mode) or data i/o (i 2 c mode). rc5/sdo bit5 st input/output port pin or synchronous serial port data output. rc6/tx/ck bit6 st input/output port pin, addressable usart asynchronous transmit, or addressable usart synchronous clock. rc7/rx/dt bit7 st input/output port pin, addressable usart asynchronous receive, or addressable usart synchronous data. legend: st = schmitt trigger input name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portc rc7 rc6 rc5 rc4 rc3 * * rc0 xxxx xxxx uuuu uuuu latc latc7 latc6 latc5 latc4 latc3 * * latc0 xxxx xxxx uuuu uuuu trisc trisc7 trisc6 trisc5 trisc4 trisc3 * * trisc0 1111 1111 1111 1111 legend: x = unknown, u = unchanged. shaded cells are not used by portc. * reserved bits; do not modify.
? 2002 microchip technology inc. preliminary ds30485a-page 91 pic18fxx39 9.4 portd, trisd and latd registers this section is applicable only to the pic18f4x39 devices. portd is an 8-bit wide, bi-directional port. the corre- sponding data direction register is trisd. setting a trisd bit (= 1) will make the corresponding portd pin an input (i.e., put the corresponding output driver in a high impedance mode). clearing a trisd bit (= 0) will make the corresponding portd pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (latd) is also memory mapped. read-modify-write operations on the latd register reads and writes the latched output value for portd. portd is an 8-bit port with schmitt trigger input buff- ers. each pin is individually configurable as an input or output. portd can be configured as an 8-bit wide micropro- cessor port (parallel slave port) by setting control bit pspmode (trise<4>). in this mode, the input buffers are ttl. see section 9.6 for additional information on the parallel slave port (psp). example 9-4: initializing portd figure 9-8: portd block diagram in i/o port mode note: on a power-on reset, these pins are configured as digital inputs. clrf portd ; initialize portd by ; clearing output ; data latches clrf latd ; alternate method ; to clear output ; data latches movlw 0xcf ; value used to ; initialize data ; direction movwf trisd ; set rd<3:0> as inputs ; rd<5:4> as outputs ; rd<7:6> as inputs data bus wr latd wr trisd rd portd data latch tris latch rd trisd schmitt trigger input buffer i/o pin (1) q d ck q d ck en qd en rd latd or portd note 1: i/o pins have diode protection to v dd and v ss .
pic18fxx39 ds30485a-page 92 preliminary ? 2002 microchip technology inc. table 9-7: portd functions table 9-8: summary of registers associated with portd name bit# buffer type function rd0/psp0 bit0 st/ttl (1) input/output port pin or parallel slave port bit0. rd1/psp1 bit1 st/ttl (1) input/output port pin or parallel slave port bit1. rd2/psp2 bit2 st/ttl (1) input/output port pin or parallel slave port bit2. rd3/psp3 bit3 st/ttl (1) input/output port pin or parallel slave port bit3. rd4/psp4 bit4 st/ttl (1) input/output port pin or parallel slave port bit4. rd5/psp5 bit5 st/ttl (1) input/output port pin or parallel slave port bit5. rd6/psp6 bit6 st/ttl (1) input/output port pin or parallel slave port bit6. rd7/psp7 bit7 st/ttl (1) input/output port pin or parallel slave port bit7. legend: st = schmitt trigger input, ttl = ttl input note 1: input buffers are schmitt triggers when in i/o mode and ttl buffers when in parallel slave port mode. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portd rd7 rd6 rd5 rd4 rd3 rd2 rd1 rd0 xxxx xxxx uuuu uuuu latd latd data output register xxxx xxxx uuuu uuuu trisd portd data direction register 1111 1111 1111 1111 trise ibf obf ibov pspmode ? porte data direction bits 0000 -111 0000 -111 legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used by portd.
? 2002 microchip technology inc. preliminary ds30485a-page 93 pic18fxx39 9.5 porte, trise and late registers this section is only applicable to the pic18f4x39 devices. porte is a 3-bit wide, bi-directional port. the corre- sponding data direction register is trise. setting a trise bit (= 1) will make the corresponding porte pin an input (i.e., put the corresponding output driver in a high impedance mode). clearing a trise bit (= 0) will make the corresponding porte pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (late) is also memory mapped. read-modify-write operations on the late register reads and writes the latched output value for porte. porte has three pins (re0/an5/rd , re1/an6/wr and re2/an7/cs ) which are individually configurable as inputs or outputs. these pins have schmitt trigger input buffers. register 9-1 shows the trise register, which also controls the parallel slave port operation. porte pins are multiplexed with analog inputs. when selected as an analog input, these pins will read as '0's. trise controls the direction of the re pins, even when they are being used as analog inputs. the user must make sure to keep the pins configured as inputs when using them as analog inputs. example 9-5: initializing porte figure 9-9: porte block diagram in i/o port mode note: on a power-on reset, these pins are configured as analog inputs. clrf porte ; initialize porte by ; clearing output ; data latches clrf late ; alternate method ; to clear output ; data latches movlw 0x07 ; configure a/d movwf adcon1 ; for digital inputs movlw 0x05 ; value used to ; initialize data ; direction movwf trise ; set re<0> as inputs ; re<1> as outputs ; re<2> as inputs data bus wr late wr trise rd porte data latch tris latch rd trise schmitt trigger input buffer q d ck q d ck en qd en i/o pin (1) rd late or porte to analog converter note 1: i/o pins have diode protection to v dd and v ss .
pic18fxx39 ds30485a-page 94 preliminary ? 2002 microchip technology inc. register 9-1: trise register r-0 r-0 r/w-0 r/w-0 u-0 r/w-1 r/w-1 r/w-1 ibf obf ibov pspmode ? trise2 trise1 trise0 bit 7 bit 0 bit 7 ibf: input buffer full status bit 1 = a word has been received and waiting to be read by the cpu 0 = no word has been received bit 6 obf : output buffer full status bit 1 = the output buffer still holds a previously written word 0 = the output buffer has been read bit 5 ibov : input buffer overflow detect bit (in microprocessor mode) 1 = a write occurred when a previously input word has not been read (must be cleared in software) 0 = no overflow occurred bit 4 pspmode : parallel slave port mode select bit 1 = parallel slave port mode 0 = general purpose i/o mode bit 3 unimplemented: read as '0' bit 2 trise2 : re2 direction control bit 1 = input 0 = output bit 1 trise1 : re1 direction control bit 1 = input 0 = output bit 0 trise0 : re0 direction control bit 1 = input 0 = output legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 95 pic18fxx39 table 9-9: porte functions table 9-10: summary of registers associated with porte name bit# buffer type function re0/an5/rd bit0 st/ttl (1) input/output port pin or analog input or read control input in parallel slave port mode for rd (psp mode): 1 = not a read operation 0 = read operation. reads portd register (if chip selected). re1/an6/wr bit1 st/ttl (1) input/output port pin or analog input or write control input in parallel slave port mode for wr (psp mode): 1 = not a write operation 0 = write operation. writes portd register (if chip selected). re2/an7/cs bit2 st/ttl (1) input/output port pin or analog input or chip select control input in parallel slave port mode for cs (psp mode): 1 = device is not selected 0 = device is selected legend: st = schmitt trigger input, ttl = ttl input note 1: input buffers are schmitt triggers when in i/o mode and ttl buffers when in parallel slave port mode. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets porte ? ? ? ? ? re2 re1 re0 ---- -000 ---- -000 late ? ? ? ? ? late data output register ---- -xxx ---- -uuu trise ibf obf ibov pspmode ? porte data direction bits 0000 -111 0000 -111 adcon1 adfm adcs2 ? ? pcfg3 pcfg2 pcfg1 pcfg0 00-- 0000 00-- 0000 legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used by porte.
pic18fxx39 ds30485a-page 96 preliminary ? 2002 microchip technology inc. 9.6 parallel slave port the parallel slave port is implemented on the 40-pin devices only (pic18f4x39). portd also operates as an 8-bit wide parallel slave port, or microprocessor port, when control bit pspmode (trise<4>) is set. it is asynchronously readable and writable by the external world through rd control input pin, re0/an5/rd and wr control input pin, re1/an6/wr . the psp can directly interface to an 8-bit microproces- sor data bus. the external microprocessor can read or write the portd latch as an 8-bit latch. setting bit pspmode enables port pin re0/an5/rd to be the rd input, re1/an6/wr to be the wr input and re2/an7/ cs to be the cs (chip select) input. for this functional- ity, the corresponding data direction bits of the trise register (trise<2:0>) must be configured as inputs (set). the a/d port configuration bits, pcfg2:pcfg0 (adcon1<2:0>), must be set, which will configure pins re2:re0 as digital i/o. a write to the psp occurs when both the cs and wr lines are first detected low. a read from the psp occurs when both the cs and rd lines are first detected low. the porte i/o pins become control inputs for the microprocessor port when bit pspmode (trise<4>) is set. in this mode, the user must make sure that the trise<2:0> bits are set (pins are configured as digital inputs), and the adcon1 is configured for digital i/o. in this mode, the input buffers are ttl. figure 9-10: portd and porte block diagram (parallel slave port) figure 9-11: parallel slave port write waveforms data bus wr latd rdx q d ck en qd en rd portd pin one bit of portd set interrupt flag pspif (pir1<7>) read chip select write rd cs wr note: i/o pin has protection diodes to v dd and v ss . ttl ttl ttl ttl or portd rd latd data latch tris latch q1 q2 q3 q4 cs q1 q2 q3 q4 q1 q2 q3 q4 wr rd ibf obf pspif portd<7:0>
? 2002 microchip technology inc. preliminary ds30485a-page 97 pic18fxx39 figure 9-12: parallel slave port read waveforms table 9-11: registers associated with parallel slave port q1 q2 q3 q4 cs q1 q2 q3 q4 q1 q2 q3 q4 wr ibf pspif rd obf portd<7:0> name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portd port data latch when written; port pins when read xxxx xxxx uuuu uuuu latd latd data output bits xxxx xxxx uuuu uuuu trisd portd data direction bits 1111 1111 1111 1111 porte ? ? ? ? ?re2re1re0 ---- -000 ---- -000 late ? ? ? ? ? late data output bits ---- -xxx ---- -uuu trise ibf obf ibov pspmode ? porte data direction bits 0000 -111 0000 -111 intcon gie/ gieh peie/ giel tmr0if int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 adcon1 adfm adcs2 ? ? pcfg3 pcfg2 pcfg1 pcfg0 00-- 0000 00-- 0000 legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used by the parallel slave port.
pic18fxx39 ds30485a-page 98 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 99 pic18fxx39 10.0 timer0 module the timer0 module has the following features: ? software selectable as an 8-bit or 16-bit timer/counter ? readable and writable ? dedicated 8-bit software programmable prescaler ? clock source selectable to be external or internal ? interrupt-on-overflow from ffh to 00h in 8-bit mode and ffffh to 0000h in 16-bit mode ? edge select for external clock figure 10-1 shows a simplified block diagram of the timer0 module in 8-bit mode and figure 10-2 shows a simplified block diagram of the timer0 module in 16-bit mode. the t0con register (register 10-1) is a readable and writable register that controls all the aspects of timer0, including the prescale selection. register 10-1: t0con: timer0 control register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 bit 7 bit 0 bit 7 tmr0on: timer0 on/off control bit 1 = enables timer0 0 = stops timer0 bit 6 t08bit : timer0 8-bit/16-bit control bit 1 = timer0 is configured as an 8-bit timer/counter 0 = timer0 is configured as a 16-bit timer/counter bit 5 t0cs : timer0 clock source select bit 1 = transition on t0cki pin 0 = internal instruction cycle clock (clko) bit 4 t0se : timer0 source edge select bit 1 = increment on high-to-low transition on t0cki pin 0 = increment on low-to-high transition on t0cki pin bit 3 psa : timer0 prescaler assignment bit 1 = timer0 prescaler is not assigned. timer0 clock input bypasses prescaler. 0 = timer0 prescaler is assigned. timer0 clock input comes from prescaler output. bit 2-0 t0ps2:t0ps0: timer0 prescaler select bits 111 = 1:256 prescale value 110 = 1:128 prescale value 101 = 1:64 prescale value 100 = 1:32 prescale value 011 = 1:16 prescale value 010 = 1:8 prescale value 001 = 1:4 prescale value 000 = 1:2 prescale value legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 100 preliminary ? 2002 microchip technology inc. figure 10-1: timer0 block diagram in 8-bit mode figure 10-2: timer0 block diagram in 16-bit mode note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki max. prescale. ra4/t0cki pin t0se 0 1 1 0 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) data bus 8 psa t0ps2, t0ps1, t0ps0 set interrupt flag bit tmr0if on overflow 3 note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki max. prescale. t0cki pin t0se 0 1 1 0 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) data bus<7:0> 8 psa t0ps2, t0ps1, t0ps0 set interrupt flag bit tmr0if on overflow 3 tmr0 tmr0h high byte 8 8 8 read tmr0l write tmr0l
? 2002 microchip technology inc. preliminary ds30485a-page 101 pic18fxx39 10.1 timer0 operation timer0 can operate as a timer or as a counter. timer mode is selected by clearing the t0cs bit. in timer mode, the timer0 module will increment every instruction cycle (without prescaler). if the tmr0l reg- ister is written, the increment is inhibited for the follow- ing two instruction cycles. the user can work around this by writing an adjusted value to the tmr0l register. counter mode is selected by setting the t0cs bit. in counter mode, timer0 will increment, either on every rising or falling edge of pin ra4/t0cki. the increment- ing edge is determined by the timer0 source edge select bit (t0se). clearing the t0se bit selects the ris- ing edge. restrictions on the external clock input are discussed below. when an external clock input is used for timer0, it must meet certain requirements. the requirements ensure the external clock can be synchronized with the internal phase clock (t osc ). also, there is a delay in the actual incrementing of timer0 after synchronization. 10.2 prescaler an 8-bit counter is available as a prescaler for the timer0 module. the prescaler is not readable or writable. the psa and t0ps2:t0ps0 bits determine the prescaler assignment and prescale ratio. clearing bit psa will assign the prescaler to the timer0 module. when the prescaler is assigned to the timer0 module, prescale values in power-of-2 increments, from 1:2 through 1:256, are selectable. when assigned to the timer0 module, all instructions writing to the tmr0l register (e.g., clrf tmr0, movwf tmr0, bsf tmr0, etc.) will clear the prescaler count. 10.2.1 switching prescaler assignment the prescaler assignment is fully under software control; it can be changed ?on-the-fly? during program execution. 10.3 timer0 interrupt the tmr0 interrupt is generated when the tmr0 reg- ister overflows from ffh to 00h in 8-bit mode, or ffffh to 0000h in 16-bit mode. this overflow sets the tmr0if bit. the interrupt can be masked by clearing the tmr0ie bit. the tmr0ie bit must be cleared in soft- ware by the timer0 module interrupt service routine before re-enabling this interrupt. the tmr0 interrupt cannot awaken the processor from sleep, since the timer is shut-off during sleep. 10.4 16-bit mode timer reads and writes tmr0h is not the high byte of the timer/counter in 16-bit mode, but is actually a buffered version of the high byte of timer0 (see figure 10-2). the high byte of the timer0 counter/timer is not directly readable nor writable. tmr0h is updated with the contents of the high byte of timer0 during a read of tmr0l. this pro- vides the ability to read all 16 bits of timer0 without having to verify that the read of the high and low byte were valid, due to a rollover between successive reads of the high and low byte. a write to the high byte of timer0 must also take place through the tmr0h buffer register. timer0 high byte is updated with the contents of tmr0h when a write occurs to tmr0l. this allows all 16 bits of timer0 to be updated at once. table 10-1: registers associated with timer0 note: writing to tmr0l when the prescaler is assigned to timer0 will clear the prescaler count, but will not change the prescaler assignment. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets tmr0l timer0 module low byte register xxxx xxxx uuuu uuuu tmr0h timer0 module high byte register 0000 0000 0000 0000 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u t0con tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 1111 1111 1111 1111 trisa ? porta data direction register -111 1111 -111 1111 legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. shaded cells are not used by timer0.
pic18fxx39 ds30485a-page 102 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 103 pic18fxx39 11.0 timer1 module the timer1 module timer/counter has the following features: ? 16-bit timer/counter (two 8-bit registers, tmr1h and tmr1l) ? readable and writable (both registers) ? internal or external clock select ? interrupt-on-overflow from ffffh to 0000h figure 11-1 is a simplified block diagram of the timer1 module. register 11-1 details the timer1 control register, which sets the operating mode of the timer1 module. timer1 can be enabled or disabled by setting or clearing control bit tmr1on (t1con<0>). register 11-1: t1con: timer1 control register r/w-0 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 r/w-0 rd16 ? t1ckps1 t1ckps0 ? t1sync tmr1cs tmr1on bit 7 bit 0 bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer1 in one 16-bit operation 0 = enables register read/write of timer1 in two 8-bit operations bit 6 unimplemented: read as '0' bit 5-4 t1ckps1:t1ckps0: timer1 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 3 unimplemented: maintain as '0' bit 2 t1sync : timer1 external clock input synchronization select bit when tmr1cs = 1: 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr1cs = 0: this bit is ignored. timer1 uses the internal clock when tmr1cs = 0. bit 1 tmr1cs: timer1 clock source select bit 1 = external clock from pin rc0/t13cki (on the rising edge) 0 = internal clock (f osc /4) bit 0 tmr1on: timer1 on bit 1 = enables timer1 0 = stops timer1 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 104 preliminary ? 2002 microchip technology inc. 11.1 timer1 operation timer1 can operate in one of these modes: ?as a timer ? as a synchronous counter ? as an asynchronous counter the operating mode is determined by the clock select bit, tmr1cs (t1con<1>). when tmr1cs = 0, timer1 increments every instruction cycle. when tmr1cs = 1, timer1 increments on every rising edge of the external clock input. figure 11-1: timer1 block diagram figure 11-2: timer1 block diagram: 16-bit read/write mode tmr1h tmr1l t1sync tmr1cs t1ckps1:t1ckps0 sleep input f osc /4 internal clock tmr1on on/off prescaler 1, 2, 4, 8 synchronize det 1 0 0 1 synchronized clock input 2 tmr1if overflow tmr1 interrupt flag bit t13cki timer 1 tmr1l t1sync tmr1cs t1ckps1:t1ckps0 sleep input tmr1if overflow interrupt f osc /4 internal clock tmr1on on/off prescaler 1, 2, 4, 8 synchronize det 1 0 0 1 synchronized clock input 2 t13cki tmr1 flag bit high byte data bus<7:0> 8 tmr1h 8 8 8 read tmr1l write tmr1l
? 2002 microchip technology inc. preliminary ds30485a-page 105 pic18fxx39 11.2 timer1 interrupt the tmr1 register pair (tmr1h:tmr1l) increments from 0000h to ffffh and rolls over to 0000h. the tmr1 interrupt, if enabled, is generated on overflow, which is latched in interrupt flag bit tmr1if (pir1<0>). this interrupt can be enabled/disabled by setting/clearing tmr1 interrupt enable bit, tmr1ie (pie1<0>). 11.3 timer1 16-bit read/write mode timer1 can be configured for 16-bit reads and writes (see figure 11-2). when the rd16 control bit (t1con<7>) is set, the address for tmr1h is mapped to a buffer register for the high byte of timer1. a read from tmr1l will load the contents of the high byte of timer1 into the timer1 high byte buffer. this provides the user with the ability to accurately read all 16-bits of timer1 without having to determine whether a read of the high byte, followed by a read of the low byte is valid, due to a rollover between reads. a write to the high byte of timer1 must also take place through the tmr1h buffer register. timer1 high byte is updated with the contents of tmr1h when a write occurs to tmr1l. this allows a user to write all 16 bits to both the high and low bytes of timer1 at once. the high byte of timer1 is not directly readable or writ- able in this mode. all reads and writes must take place through the timer1 high byte buffer register. writes to tmr1h do not clear the timer1 prescaler. the prescaler is only cleared on writes to tmr1l. table 11-1: registers associated with timer1 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 tmr1l holding register for the least significant byte of the 16-bit tmr1 register xxxx xxxx uuuu uuuu tmr1h holding register for the most significant byte of the 16-bit tmr1 register xxxx xxxx uuuu uuuu t1con rd16 ? t1ckps1 t1ckps0 ? t1sync tmr1cs tmr1on 0-00 0000 u-uu uuuu legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used by the timer1 module. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
pic18fxx39 ds30485a-page 106 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 107 pic18fxx39 12.0 timer2 module the timer2 module is an 8-bit timer with a selectable 8-bit period. it has the following features: ? input from system clock at f osc /4 with programmable input prescaler ? interrupt on timer-to-period match with programmable postscaler the module has three registers: the tmr2 counter, the pr2 period register, and the t2con control register. the general operation of timer2 is shown in figure 12-1. additional information on the use of timer2 as a time-base is available in section 15.0 (pwm modules). figure 12-1: timer2 block diagram note: in pic18fxx39 devices, timer2 is used exclusively as a time-base for the pwm modules in motor control applications. as such, it is not available to users as a resource. although their locations are shown on the device data memory maps, none of the timer2 registers are directly accessible. users should not alter the values of these registers. comparator tmr2 sets flag tmr2 output reset postscaler prescaler pr2 f osc /4 1:1 to 1:16 1:1, 1:4, 1:16 bit tmr2if (prompt module) (tmr2 = pr2) (prompt module)
pic18fxx39 ds30485a-page 108 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 109 pic18fxx39 13.0 timer3 module the timer3 module timer/counter has the following features: ? 16-bit timer/counter (two 8-bit registers: tmr3h and tmr3l) ? readable and writable (both registers) ? internal or external clock select ? interrupt-on-overflow from ffffh to 0000h figure 13-1 is a simplified block diagram of the timer3 module. register 13-1 shows the timer1 control register, which sets the operating mode of the timer1 module. register 13-1: t3con: timer3 control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 rd16 ? t3ckps1 t3ckps0 ? t3sync tmr3cs tmr3on bit 7 bit 0 bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer3 in one 16-bit operation 0 = enables register read/write of timer3 in two 8-bit operations bit 6, 3 unimplemented: maintain as ?0? bit 5, 4 t3ckps1:t3ckps0: timer3 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 2 t3sync : timer3 external clock input synchronization control bit (not usable if the system clock comes from timer1/timer3) when tmr3cs = 1: 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr3cs = 0: this bit is ignored. timer3 uses the internal clock when tmr3cs = 0. bit 1 tmr3cs: timer3 clock source select bit 1 = external clock input from t13cki (on the rising edge after the first falling edge) 0 = internal clock (f osc /4) bit 0 tmr3on: timer3 on bit 1 = enables timer3 0 = stops timer3 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 110 preliminary ? 2002 microchip technology inc. 13.1 timer3 operation timer3 can operate in one of these modes: ?as a timer ? as a synchronous counter ? as an asynchronous counter the operating mode is determined by the clock select bit, tmr3cs (t3con<1>). when tmr3cs = 0, timer3 increments every instruction cycle. when tmr3cs = 1, timer3 increments on every rising edge of the timer1 external clock input. figure 13-1: timer3 block diagram figure 13-2: timer3 block diagram configured in 16-bit read/write mode tmr3h tmr3l t3sync tmr3cs t3ckps1:t3ckps0 sleep input tmr3if overflow interrupt f osc /4 internal clock tmr3on on/off prescaler 1, 2, 4, 8 synchronize det 1 0 0 1 synchronized clock input 2 flag bit t13cki timer3 tmr3l t3sync tmr3cs t3ckps1:t3ckps0 sleep input f osc /4 internal clock tmr3on on/off prescaler 1, 2, 4, 8 synchronize det 1 0 0 1 synchronized clock input 2 tmr3 t13cki to timer1 clock input high byte data bus<7:0> 8 tmr3h 8 8 8 read tmr3l write tmr3l set tmr3if flag bit on overflow
? 2002 microchip technology inc. preliminary ds30485a-page 111 pic18fxx39 13.2 timer3 interrupt the tmr3 register pair (tmr3h:tmr3l) increments from 0000h to ffffh and rolls over to 0000h. the tmr3 interrupt, if enabled, is generated on overflow, which is latched in interrupt flag bit, tmr3if (pir2<1>). this interrupt can be enabled/disabled by setting/clearing tmr3 interrupt enable bit, tmr3ie (pie2<1>). table 13-1: registers associated with timer3 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir2 ? ? ? eeif bclif lvdif tmr3if ? ---0 0000 ---0 0000 pie2 ? ? ? eeie bclie lvdie tmr3ie ? ---0 0000 ---0 0000 ipr2 ? ? ? eeip bclip lvdip tmr3ip ? ---1 1111 ---1 1111 tmr3l holding register for the least significant byte of the 16-bit tmr3 register xxxx xxxx uuuu uuuu tmr3h holding register for the most significant byte of the 16-bit tmr3 register xxxx xxxx uuuu uuuu t1con rd16 ? t1ckps1 t1ckps0 ? t1sync tmr1cs tmr1on 0-00 0000 u-uu uuuu t3con rd16 ? t3ckps1 t3ckps0 ? t3sync tmr3cs tmr3on 0000 0000 uuuu uuuu legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used by the timer1 module.
pic18fxx39 ds30485a-page 112 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 113 pic18fxx39 14.0 single phase induction motor control kernel the motor control kernel of the pic18fxx39 family uses programmable motor processor technology (prompt) to control the speed of a single phase induc- tion motor, with variable frequency technology. the controller?s two pwm modules are used to synthesize a sine wave current through the motor windings. the kernel provides open loop control for a continuous frequency range of 15 hz to 127 hz. 14.1 theory of operation the speed of an induction motor is a function of fre- quency, slip and the number of poles in the motor. they are related by the equation: where speed and slip are in rpm, f is the frequency of the input voltage (in hertz), and p represents the number of motor poles (for this equation, either 2, 4, 6 or 8). for the purpose of this discussion, slip is assumed to be constant across the motor?s useful operating range. since the rated speed is based on the number of poles (which is fixed at the time of manufacture), this leaves changing the frequency of the supplied voltage as the only way to vary the motor?s speed. when the fre- quency controlling a motor is reduced, however, its impedance is also reduced, resulting in a higher motor current draw. it can be shown that the voltage applied to the motor is proportional to both the frequency and the current (equation 14-1). so to keep the current constant at, or below the full load amp rating, the rms voltage to the motor must be reduced as the frequency is reduced. by varying the supply voltage and frequency at a constant ratio, the motor?s speed can be varied with constant current. maintaining this constant ratio is the function of the motor control kernel. equation 14-1: key relationships in single phase motors 14.2 typical hardware interface a block diagram for a recommended single phase induction motor control using the pic18fxx39 is shown in figure 14-1. the single phase ac supply is rectified, using a diode bridge and filtered, using a capacitor. the pwm out- puts from the pic18fxx39 synthesize the ac to drive the motor from this dc bus by switching insulated gate bipolar transistors (igbts) on and off. the igbt gate driver converts the ttl level of pwms to the required igbt gate voltage level, and supplies the gate charging current when the igbt turns on. the i/o ports of the microcontroller can be used for the external logic controls. the a/d channels can be used for monitoring the dc bus voltage and motor current; a potentiometer can also be connected to one of these channels to provide a variable frequency reference for the motor. figure 14-1: typical motor control system using the pic18fxx39 speed f 120 p ? () slip ? = where: v is applied voltage i is motor current is stator flux f is input frequency v ? v 2 f i v f --- ? (1-1) (1-2) (1-3) or: pic18fxx39 igbt driver voltage monitor current monitor igbt h-bridge motor m1 m2 g pwm1 pwm2 +15v +5v gnd + - l n g single phase ac input gate drives power rectifier mov a/d a/d i/oports supply i/o interface digital analog a/d
pic18fxx39 ds30485a-page 114 preliminary ? 2002 microchip technology inc. 14.3 software interface a sine table, stored in the prompt kernel, is used as the basis for synthesizing the dc bus using the pwm modules. the table values are accessed in sequence and scaled based on the frequency or the speed at which the motor is intended to run. the intended fre- quency input can be from an a/d channel or a digital value. parameters in the prompt modules can be accessed using the pre-defined application program interface (api) methods. a list of the apis is given in section 14.3.3. for example, to run the motor at 40 hz, the user would invoke the prommpt_setfrequency api: i = prompt_setfrequency(40); where i is an unsigned character variable. in this case, if i = 0 on return, the command has been successfully executed. if the frequency input is out of range, or if there is an error in setting the frequency, i is returned with a value of ffh. similarly, to check the frequency set by the prompt kernel, use the prompt_getfrequency api: i = prompt_getfrequency(void); where i is an unsigned character variable. upon return from the prompt kernel, i will contain the frequency value in the prompt kernel. 14.3.1 the v/f curve the prompt kernel contains a default v/f curve stored in memory. the default curve is linear, as shown in figure 14-2. table 14-1 shows the data points used to construct the curve. users may require a different v/f curve for their appli- cation, based on the load on the motor, or based on the characteristics of the motor used. the curve can be changed in the application program using the api method setvfcurve(x,y), where x is the frequency and y is the level of modulation of the dc bus voltage. as a rule, in customizing the curve, the input frequency corresponding to the point on the v/f curve that gives 100% modulation should match the motor?s rated fre- quency. similarly, full modulation should occur at the motor?s rated input voltage. (see figure 14-2 for details.) examples of the characteristics for v/f curves for typi- cal motor applications are shown in section 14-2 (page 115). 14.3.2 parameters defined by the prompt api methods frequency: the frequency (in hz) of the supply current for steady state motor operation. modulation: the level of modulation (in percentage) applied to the dc supply voltage by the pwm through the h-bridge to produce ac drive current. acceleration rate: the rate of increase of motor speed, achieved by ramping up the supply frequency. expressed in hz/s. deceleration rate: the rate of decrease of motor speed, achieved by ramping down the supply frequency. expressed in hz/s. boost: the mode for starting a stopped motor by vary- ing the supply current frequency and modulation until steady state speed is reached. boost is defined in terms of a frequency, a starting and ending modulation, and a time interval for the transition between the two. pwm frequency: the sampling rate (in khz) at which the pwm module operates. figure 14-2: default v/f curve for the prompt kernel table 14-1: data points for the default v/f curve frequency (hz) % modulation 00 814 16 28 24 42 32 57 40 71 48 86 56 100 64 110 72 133 80 133 88 133 96 133 104 133 112 133 120 133 128 133 0 25 50 75 100 125 150 0 20406080100120140 input frequency (hz) voltage modulation (%) f rated of motor should equal f at 100% modulation v rated of motor should equal at 100% modulation
? 2002 microchip technology inc. preliminary ds30485a-page 115 pic18fxx39 table 14-2: prompt output characteristics for various v/f curves motor type: rated voltage: full load current: rated frequency: rated speed: rated power: shaded pole blower 115v 3.5/3.25a 50/60 hz 1570 rpm 1/10 hp input frequency (hz) measured frequency (hz) deviation (%) measured output voltage (rms) measured output current (a) motor speed (rpm) linear v/f curve (pre-programmed) 15 14.8 1.3 22.8 1.59 348 18 17.8 1.1 28.2 1.75 445 20 19.8 1.0 33.5 1.92 505 25 24.7 1.2 42.0 2.08 651 30 29.7 1.0 52.6 2.26 794 35 34.6 1.1 62.0 2.40 926 40 39.6 1.0 72.3 2.57 1060 45 44.5 1.1 81.3 2.70 1185 50 49.5 1.0 90.7 2.79 1305 55 54.4 1.1 99.6 2.96 1421 60 59.4 1.0 107.8 3.10 1536 65 64.3 1.1 112.3 3.26 1565 70 69.3 1.0 111.5 3.53 1450 75 74.2 1.1 111.3 3.69 1070 pump v/f curve 15 14.8 1.3 15.0 1.00 3.5 18 17.8 1.1 18.4 1.10 396 20 19.8 1.0 21.4 1.23 456 25 24.7 1.2 29.5 1.44 602 30 29.7 1.0 36.6 1.60 722 35 34.6 1.1 44.7 1.79 852 40 39.6 1.0 53.9 2.01 979 45 44.5 1.1 62.9 2.21 1092 50 49.5 1.0 73.4 2.47 1221 55 54.4 1.1 88.2 2.79 1367 60 59.4 1.0 102.0 3.05 1488 65 64.3 1.1 108.8 3.25 1538 70 69.3 1.0 108.0 3.50 1385 75 74.3 0.9 109.1 3.58 994
pic18fxx39 ds30485a-page 116 preliminary ? 2002 microchip technology inc. strong fan v/f curve 15 14.8 1.3% 6.2 0.45 100 18 17.8 1.1% 8.5 0.57 193 20 19.8 1.0% 11.3 0.69 264 25 24.7 1.2% 17.3 0.94 408 30 29.7 1.0% 24.0 1.17 538 35 34.6 1.1% 31.5 1.43 654 40 39.6 1.0% 38.9 1.66 720 45 44.5 1.1% 49.5 1.96 888 50 49.5 1.0% 61.6 2.26 1040 55 54.4 1.1% 73.5 2.56 1162 60 59.4 1.0% 93.8 2.94 1410 65 64.3 1.1% 106.8 3.24 1534 70 69.3 1.0% 108.9 3.49 1401 75 74.2 1.1% 109.5 3.58 1016 weak fan v/f curve 15 14.8 1.3% 14.9 0.99 306 18 17.8 1.1% 19.1 1.15 405 20 19.8 1.0% 23.5 1.31 475 25 24.7 1.2% 32.8 1.56 619 30 29.7 1.0% 41.2 1.79 759 35 34.6 1.1% 51.5 2.01 893 40 39.6 1.0% 62.2 2.23 1018 45 44.5 1.1% 73.7 2.47 1155 50 49.4 1.2% 83.0 2.64 1277 55 54.4 1.1% 92.5 2.86 1397 60 59.4 1.0% 103.5 3.06 1498 65 64.3 1.1% 108.0 3.22 1500 70 69.3 1.0% 107.8 3.50 1348 75 74.2 1.1% 108.1 3.55 949 table 14-2: prompt output characteristics for various v/f curves (continued) motor type: rated voltage: full load current: rated frequency: rated speed: rated power: shaded pole blower 115v 3.5/3.25a 50/60 hz 1570 rpm 1/10 hp input frequency (hz) measured frequency (hz) deviation (%) measured output voltage (rms) measured output current (a) motor speed (rpm)
? 2002 microchip technology inc. preliminary ds30485a-page 117 pic18fxx39 14.3.3 prompt api methods there are 27 separate api methods for the prompt kernel: void prompt_cleartick(void) resources used: 0 stack levels description: this function clears the tick (62.5 ms) timer flag returned by prompt_tick() . this function must be called by any routine that is used for timing purposes. void prompt_disableboostmode(void) resources used: 0 stack levels description: this function disables the boost mode logic. this method should be called before changing any of the boost mode parameters. void prompt_enableboostmode(void) resources used: 0 stack levels description: this function enables the boost mode logic. boost mode is entered when a stopped drive is commanded to start. the drive will immediately go to boost frequency and ramp from start modulation to end modulation over the time period, boost time. unsigned char prompt_getaccelrate(void) resources used: 1 stack level range of values: 0 to 255 description: returns the current acceleration rate in hz/second. unsigned char prompt_getboostendmodulation(void) resources used: 1 stack level range of values : 0 to 200 description: returns the current end modulation (in %) used in the boost logic. unsigned char prompt_getboostfrequency(void) resources used: 1 stack level range of values: 0 to 127 description: returns the current boost frequency in hz. unsigned char prompt_getbooststartmodulation(void) resources used: 1 stack level range of values: 0 to boostendmodulation description: returns the start modulation (in %) used in the boost logic. note: the operation of the motor control kernel and its apis is based on an assumed clock frequency of 20 mhz. changing the oscillator frequency will change the timing used in the motor control kernel accordingly. to achieve the best results in motor control applications, a clock frequency of 20 mhz is highly recommended.
pic18fxx39 ds30485a-page 118 preliminary ? 2002 microchip technology inc. unsigned char prompt_getboosttime() resources used: 1 stack level range of values: 0 to 255 description: returns the time in seconds for boost mode. unsigned char prompt_getdecelrate() resources used: 1 stack level range of values: 0 to 255 description: returns the current deceleration rate in hz/second. unsigned char prompt_getfrequency(void) resources used: 1 stack level range of values: 0 to 127 description: returns the current output frequency in hz. this may not be the frequency commanded due to boost or accel/decel logic. unsigned char prompt_getmodulation(void) resources used: hardware multiplier; 1 stack level range of values: 0 to 200 description: returns the current output modulation in %. unsigned char prompt_getparameter(unsigned char parameter) resources used: 1 stack level description: in addition to its pre-defined api methods, the prompt kernel allows the user to custom define up to 16 functions for control or communication purposes not covered by the prompt apis. these parameters are used to com- municate with motor control gui evaluation tools, such as microchip?s dashdrivemp tm . this method returns the current value of any one of the parameters. unsigned char prompt_getvfcurve(unsigned char point) resources used: hardware multiplier; 1 stack level description: this function returns one of the 17 modulation values (in %) of the v/f curve. each point represents a frequency increment of 8 hz, ranging from point 0 (0 hz) to point 16 (128 hz). void prompt_init(unsigned char pwmfrequency) resources used: 64 bytes ram; timer2; pwm1 and pwm2; high priority interrupt vector; hardware multiplier; fast call/return; fsr 0; tblptr; 2 stack levels pwmfrequency values: 0 or 1 description: this function must be called before all other prompt methods, and it must be called only once. this routine configures timer2 and the pwm outputs. when pwmfrequency is ?0?, the module?s operating frequency is 9.75 khz. when pwmfrequency is ?1?, the module?s operating frequency is 19.53 khz. note: since the high priority interrupt is used, the fast call/return cannot be used by other routines.
? 2002 microchip technology inc. preliminary ds30485a-page 119 pic18fxx39 void prompt_setaccelrate(unsigned char rate) resources used: 0 stack level rate range: 0 to 255 description: sets the acceleration to the value of rate in hz/second. the default setting is 10 hz/s. void prompt_setboostendmodulation(unsigned char modulation) resources used: hardware multiplier; 0 stack levels modulation range: 0 to 200 description: sets the end modulation (in %) for the boost logic. boost mode operates at boost frequency, and the modulation ramps from booststartmodulation to boostendmodulation . this function should not be called while boost is enabled. unsigned char prompt_setboostfrequency(unsigned char frequency) resources used : 0 stack levels frequency range: 0 to 127 description: sets the frequency the drive goes to in boost mode. frequency must be < 128. on exit, w = 0 if the command is successful, or w = ffh if the frequency is out of range. this function should not be called while boost is enabled. void prompt_setbooststartmodulation(unsigned char modulation) resources used: hardware multiplier; 0 stack levels modulation range: 0 to boostendmodulation description: sets the start modulation (in %) for the boost logic. boost mode operates at boost frequency, and the modulation ramps from booststartmodulation to boostendmodulation . this function should not be called while boost is enabled. void prompt_setboosttime(unsigned char time) resources used: hardware multiplier; 0 stack levels time range: 0 to 255 description: sets the amount of time in seconds for the boost mode. boost mode operates at boost frequency, and the modulation ramps from booststartmodulation to boostendmodulation over boosttime . this function should not be called while boost is enabled. void prompt_setdecelrate(unsigned char rate) resources used: 0 stack levels rate range: 0 to 255 description: sets the deceleration to the value of rate in hz per second. the default setting is 5 hz/s. unsigned char prompt_setfrequency(unsigned char frequency) resources used: 2 stack levels frequency range: 0 to 127 description: sets the output frequency of the drive if the drive is running. frequency is limited to 0 to 127, but should be controlled within the valid operational range of the motor. modulation is determined from the v/f curve, which is set up with the prompt_setvfcurve method. if frequency = 0, the drive will stop. if the drive is stopped and frequency > 0, the drive will start.
pic18fxx39 ds30485a-page 120 preliminary ? 2002 microchip technology inc. void prompt_setlinevoltage(unsigned char voltage) resources used: hardware multiplier; 0 stack levels voltage range: 0 to 255 description: sets the line voltage for automatic voltage compensation. the units for setlinevoltage and setmotorvoltage must be the same for accurate operation. the values passed to setmotorvoltage and setlinevoltage can be the same to disable voltage compensation. void prompt_setmotorvoltage(unsigned char voltage) resources used: hardware multiplier; 0 stack levels voltage range: 0 to 255 description: sets the motor rating for automatic voltage compensation. the units for setlinevoltage and setmotorvoltage must be the same for accurate operation. the values passed to setmotorvoltage and setlinevoltage can be the same to disable voltage compensation. void prompt_setparameter(unsigned char parameter, unsigned char value) resources used : 0 stack levels parameter range : description: in addition to its pre-defined api methods, the prompt kernel allows the user to custom define up to 16 functions for control or communication purposes not covered by the prompt apis. this function sets the value of the specified user defined function. void prompt_setpwmfrequency(unsigned char pwmfrequency) pwmfrequency values: 0 or 1 resources used: timer2; 1 stack level description: this sets and changes the pwm switching frequency. typically, this is set with the init() function. when pwmfrequency is ?0?, the module?s operating frequency is 9.75 khz. when pwmfrequency is ?1?, the module?s operating frequency is 19.53 khz. void prompt_setvfcurve(unsigned char point, unsigned char value) resources used: hardware multiplier; 0 stack level point range: 0 to 16 (0 = 0 hz, 1 = 8 hz, 2 = 16 hz??. 17 = 128 hz) value range: 0 to 200 description: this sets one of the 17 modulation values (in %) for the v/f curve. each point represents a frequency increment of 8 hz, ranging from point 0 (0 hz) to point 16 (128 hz). unsigned char prompt_tick(void) resources used: 1 stack level description: the value of the tick timer flag becomes ?1? every 62.5 ms (1/16 second). this can be used for timing applications. cleartick must be called in the timing routine when this is serviced.
? 2002 microchip technology inc. preliminary ds30485a-page 121 pic18fxx39 14.4 developing applications using the motor control kernel the motor control kernel allows users to develop their applications without having knowledge of motor con- trol. the key parameters of the motor control kernel can be set and read through the application program inter- face (api) methods discussed in the previous section. the overall application can be thought of as a protocol stack, as shown in figure 14-3. in this case, the api methods reside between the user?s application and the prompt kernel, and are used to exchange parameter values. the motor control kernel sets the pwm duty cycles based on the inputs from the application software. a typical motor control routine is shown in example 14-1. in this case, the motor will run at 20 hz for 10 seconds, accelerate to 60 hz at the rate of 10 hz/s, remain at 60 hz for 20 seconds, and finally stop. figure 14-3: layers of the motor control architecture stack example 14-1: motor control routine using the prompt apis prompt motor hardware application program interface (api) application software parameters control kernel methods and user interface void main() { unsigned char i; unsigned char j; prompt_init(0); // initialize the prompt block i = prompt_setfrequency(10); // set motor frequency to 10hz for (i=0;i<161;i++) // set counter for 10 sec @ 1/16 sec per tick { j = prompt_tick(void); // tick of 1/16 sec prompt_cleartick(void); // clearing the tick flag } prompt_setaccelrate(10); // set acceleration rate to 10 hz/sec i = prompt_setfrequency(60); // set motor frequency to 60 hz for (i=0;i<161;i++) // set counter for 20 sec @ 1/16 sec per tick { // (2 loops of 10 sec each) j = prompt_tick(void); // tick of 1/16 sec prompt_cleartick(void); // clearing the tick flag j = prompt_tick(void); // tick of 1/16 sec prompt_cleartick(void); // clearing the tick flag } i = prompt_setfrequency(0); // set motor frequency to 0 hz (stop) while(1); } // end of the task
pic18fxx39 ds30485a-page 122 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 123 pic18fxx39 15.0 pulse width modulation (pwm) modules pic18fxx39 devices are equipped with two 10-bit pwm modules. each contains a register pair (ccpxh:ccpxl), which operates as a master/slave duty cycle register, and a control register (ccpxcon). the modules use timer2 (section 12.0) as their time- base reference. figure 15-1 shows a simplified block diagram of the module?s operation. this section gives a brief overview of pwm operation as controlled by the motor control module (section 14.0). operation is described with respect to pwm1, but is equally applicable to pwm2. 15.1 pwm mode in pulse width modulation, each pwm pin produces a pwm output with a resolution of up to 10 bits. a pwm output (figure 15-2) has a time-base (period) and a time that the output stays high (duty cycle). the frequency of the pwm is the inverse of the period (1/period). figure 15-1: simplified pwm block diagram figure 15-2: pwm output 15.1.1 pwm period the pwm period is specified when the motor control module is initialized. the pwm period can be calculated using the formula: pwm period = [(pr2) + 1] ? 4 ? t osc ? (tmr2 prescale value) pwm frequency is defined as 1 / [pwm period]. the api method void prompt_init (page 118) sets the required pwm frequency in the application. the parameter pwmfrequency determines the operat- ing frequency of the module. when it is ?0?, the pwm frequency set in the motor control module is 9.75 khz; when it is ?1?, the set pwm frequency is 19.53 khz. when tmr2 is equal to pr2, the following three events occur on the next increment cycle: ?tmr2 is cleared ? the pwm1 pin is set (exception: if pwm duty cycle = 0%, the pwm1 pin will not be set) ? the pwm duty cycle is latched from ccpr1l into ccpr1h note: the pwm modules are used exclusively by the motor control module. as such, they are not available to users as a separate resource. although their locations are shown on the device data memory maps, users should not modify the values of these registers. ccpr1l ccpr1h (slave) comparator tmr2 comparator pr2 (1) rq s duty cycle registers ccp1con<5:4> clear timer, pwm1 pin and latch duty cycle pwm1 note 1: 8-bit timer is concatenated with 2-bit internal q clock, or 2 bits of the prescaler to create a 10-bit time-base. note: the timer2 postscaler (see section 12.0) is not used in the determination of the pwm frequency. the postscaler could be used to have a servo update rate at a different frequency than the pwm output. period duty cycle tmr2 = pr2 tmr2 = duty cycle tmr2 = pr2
pic18fxx39 ds30485a-page 124 preliminary ? 2002 microchip technology inc. 15.1.2 pwm duty cycle the pwm duty cycle is set by the motor control module when it writes a 10-bit value to the ccpr1l and ccp1con registers, where ccpr1l contains the eight most significant bits and ccp1con<5:4> con- tains the two least significant bits. the duty cycle time is given by the equation: pwm duty cycle = (10-bit ccp register value) ? t osc ? (tmr2 prescale value) where t osc and the duty cycle are in the same unit of time. the ccpr1h register and a 2-bit internal latch are used to double-buffer the pwm duty cycle. this buffer- ing is essential for glitchless pwm operation. at the same time, the value of tmr2 is concatenated with either an internal 2-bit q clock, or 2 bits of the tmr2 prescaler. when the ccpr1h:latch pair value matches that of the tmr2:latch pair, the pwm1 pin is cleared. the maximum pwm resolution (bits) for a given pwm frequency is given by the equation: where f pwm is the pwm frequency, or (1/pwm period). table 15-1: registers associated with pwm and timer2 note: if the pwm duty cycle value is longer than the pwm period, the pwm1 pin will not be cleared. f osc f pwm --------------- ?? ?? log 2 () log ----------------------------- b i t s = pwm resolution (max) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 tmr2 * * * * * * * * * 0000 0000 0000 0000 pr2 * * * * * * * * * 1111 1111 1111 1111 t2con * * * * * * * * * -000 0000 -000 0000 ccpr1l * * * * * * * * * xxxx xxxx uuuu uuuu ccpr1h pwm register1 (msb) (read-only) xxxx xxxx uuuu uuuu ccp1con * ? ? * * * * * * --00 0000 --00 0000 ccpr2l * * * * * * * * * xxxx xxxx uuuu uuuu ccpr2h * pwm register2 (msb) (read-only) xxxx xxxx uuuu uuuu ccp2con * ? ? * * * * * * --00 0000 --00 0000 legend: x = unknown, u = unchanged, - = unimplemented, read as '0' unless otherwise noted. shaded cells are not used by pwm and timer2. * these registers are retained to maintain compatibility with pic18fxx2 devices; however, the indicated bits are reserved in pic18fxx39 devices. users should not alter the values of these bits. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
? 2002 microchip technology inc. preliminary ds30485a-page 125 pic18fxx39 16.0 master synchronous serial port (mssp) module 16.1 master ssp (mssp) module overview the master synchronous serial port (mssp) module is a serial interface useful for communicating with other peripheral or microcontroller devices. these peripheral devices may be serial eeproms, shift registers, dis- play drivers, a/d converters, etc. the mssp module can operate in one of two modes: ? serial peripheral interface (spi) ? inter-integrated circuit (i 2 c) - full master mode - slave mode (with general address call) the i 2 c interface supports the following modes in hardware: ?master mode ? multi-master mode ? slave mode 16.2 control registers the mssp module has three associated registers. these include a status register (sspstat) and two control registers (sspcon1 and sspcon2). the use of these registers and their individual configuration bits differ significantly, depending on whether the mssp module is operated in spi or i 2 c mode. additional details are provided under the individual sections. 16.3 spi mode the spi mode allows 8 bits of data to be synchronously transmitted and received, simultaneously. all four modes of spi are supported. to accomplish communication, typically three pins are used: ? serial data out (sdo) - rc5/sdo ? serial data in (sdi) - rc4/sdi/sda ? serial clock (sck) - rc3/sck/scl additionally, a fourth pin may be used when in a slave mode of operation: ? slave select (ss ) - ra5/an4/ss /lvdin figure 16-1 shows the block diagram of the mssp module when operating in spi mode. figure 16-1: mssp block diagram (spi mode) read write internal data bus sspsr reg sspm3:sspm0 bit0 shift clock ss control enable edge select clock select t osc prescaler 4 / 16 / 64 edge select 2 4 data to tx/rx in sspsr tris bit 2 smp:cke rc5/sdo sspbuf reg rc4/sdi/sda ra5/an4/ss / rc3/sck/ scl lvdin
pic18fxx39 ds30485a-page 126 preliminary ? 2002 microchip technology inc. 16.3.1 registers the mssp module has four registers for spi mode operation. these are: ? mssp control register1 (sspcon1) ? mssp status register (sspstat) ? serial receive/transmit buffer (sspbuf) ? mssp shift register (sspsr) - not directly accessible sspcon1 and sspstat are the control and status registers in spi mode operation. the sspcon1 regis- ter is readable and writable. the lower 6 bits of the sspstat are read only. the upper two bits of the sspstat are read/write. sspsr is the shift register used for shifting data in or out. sspbuf is the buffer register to which data bytes are written to or read from. in receive operations, sspsr and sspbuf together create a double-buffered receiver. when sspsr receives a complete byte, it is transferred to sspbuf and the sspif interrupt is set. during transmission, the sspbuf is not double-buffered. a write to sspbuf will write to both sspbuf and sspsr. register 16-1: sspstat: mssp status register (spi mode) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a psr/w ua bf bit 7 bit 0 bit 7 smp: sample bit spi master mode: 1 = input data sampled at end of data output time 0 = input data sampled at middle of data output time spi slave mode: smp must be cleared when spi is used in slave mode bit 6 cke: spi clock edge select bit when ckp = 0: 1 = data transmitted on rising edge of sck 0 = data transmitted on falling edge of sck when ckp = 1: 1 = data transmitted on falling edge of sck 0 = data transmitted on rising edge of sck bit 5 d/a : data/address bit used in i 2 c mode only bit 4 p: stop bit used in i 2 c mode only. this bit is cleared when the mssp module is disabled, sspen is cleared. bit 3 s: start bit used in i 2 c mode only bit 2 r/w : read/write bit information used in i 2 c mode only bit 1 ua: update address used in i 2 c mode only bit 0 bf: buffer full status bit (receive mode only) 1 = receive complete, sspbuf is full 0 = receive not complete, sspbuf is empty legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 127 pic18fxx39 register 16-2: sspcon1: mssp control register 1 (spi mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 bit 7 wcol: write collision detect bit (transmit mode only) 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared in software) 0 = no collision bit 6 sspov: receive overflow indicator bit spi slave mode: 1 = a new byte is received while the sspbuf register is still holding the previous data. in case of overflow, the data in sspsr is lost. overflow can only occur in slave mode.the user must read the sspbuf, even if only transmitting data, to avoid setting overflow (must be cleared in software). 0 = no overflow note: in master mode, the overflow bit is not set since each new reception (and transmission) is initiated by writing to the sspbuf register. bit 5 sspen: synchronous serial port enable bit 1 = enables serial port and configures sck, sdo, sdi, and ss as serial port pins 0 = disables serial port and configures these pins as i/o port pins note: when enabled, these pins must be properly configured as input or output. bit 4 ckp: clock polarity select bit 1 = idle state for clock is a high level 0 = idle state for clock is a low level bit 3-0 sspm3:sspm0: synchronous serial port mode select bits 0101 = spi slave mode, clock = sck pin, ss pin control disabled, ss can be used as i/o pin 0100 = spi slave mode, clock = sck pin, ss pin control enabled 0011 = reserved 0010 = spi master mode, clock = f osc /64 0001 = spi master mode, clock = f osc /16 0000 = spi master mode, clock = f osc /4 note: bit combinations not specifically listed here are either reserved, or implemented in i 2 c mode only. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 128 preliminary ? 2002 microchip technology inc. 16.3.2 operation when initializing the spi, several options need to be specified. this is done by programming the appropriate control bits (sspcon1<5:0>) and sspstat<7:6>. these control bits allow the following to be specified: ? master mode (sck is the clock output) ? slave mode (sck is the clock input) ? clock polarity (idle state of sck) ? data input sample phase (middle or end of data output time) ? clock edge (output data on rising/falling edge of sck) ? clock rate (master mode only) ? slave select mode (slave mode only) the mssp consists of a transmit/receive shift register (sspsr) and a buffer register (sspbuf). the sspsr shifts the data in and out of the device, msb first. the sspbuf holds the data that was written to the sspsr, until the received data is ready. once the 8 bits of data have been received, that byte is moved to the sspbuf register. then, the buffer full detect bit, bf (sspstat<0>), and the interrupt flag bit, sspif, are set. this double-buffering of the received data (sspbuf) allows the next byte to start reception before reading the data that was just received. any write to the sspbuf register during transmission/reception of data will be ignored, and the write collision detect bit, wcol (sspcon1<7>), will be set. user software must clear the wcol bit so that it can be determined if the follow- ing write(s) to the sspbuf register completed successfully. when the application software is expecting to receive valid data, the sspbuf should be read before the next byte of data to transfer is written to the sspbuf. buffer full bit, bf (sspstat<0>), indicates when sspbuf has been loaded with the received data (transmission is complete). when the sspbuf is read, the bf bit is cleared. this data may be irrelevant if the spi is only a transmitter. generally, the mssp interrupt is used to determine when the transmission/reception has com- pleted. the sspbuf must be read and/or written. if the interrupt method is not going to be used, then software polling can be done to ensure that a write collision does not occur. example 16-1 shows the loading of the sspbuf (sspsr) for data transmission. the sspsr is not directly readable or writable, and can only be accessed by addressing the sspbuf reg- ister. additionally, the mssp status register (sspstat) indicates the various status conditions. example 16-1: loading the sspbuf (sspsr) register loop btfss sspstat, bf ;has data been received(transmit complete)? bra loop ;no movf sspbuf, w ;wreg reg = contents of sspbuf movwf rxdata ;save in user ram, if data is meaningful movf txdata, w ;w reg = contents of txdata movwf sspbuf ;new data to xmit
? 2002 microchip technology inc. preliminary ds30485a-page 129 pic18fxx39 16.3.3 enabling spi i/o to enable the serial port, ssp enable bit, sspen (sspcon1<5>), must be set. to reset or reconfigure spi mode, clear the sspen bit, re-initialize the sspcon registers, and then set the sspen bit. this configures the sdi, sdo, sck, and ss pins as serial port pins. for the pins to behave as the serial port func- tion, some must have their data direction bits (in the tris register) appropriately programmed. that is: ? sdi is automatically controlled by the spi module ? sdo must have trisc<5> bit cleared ? sck (master mode) must have trisc<3> bit cleared ? sck (slave mode) must have trisc<3> bit set ?ss must have trisc<4> bit set any serial port function that is not desired may be overridden by programming the corresponding data direction (tris) register to the opposite value. 16.3.4 typical connection figure 16-2 shows a typical connection between two microcontrollers. the master controller (processor 1) initiates the data transfer by sending the sck signal. data is shifted out of both shift registers on their pro- grammed clock edge, and latched on the opposite edge of the clock. both processors should be pro- grammed to the same clock polarity (ckp), then both controllers would send and receive data at the same time. whether the data is meaningful (or dummy data) depends on the application software. this leads to three scenarios for data transmission: ? master sends data ? slave sends dummy data ? master sends data ? slave sends data ? master sends dummy data ? slave sends data figure 16-2: spi master/slave connection serial input buffer (sspbuf) shift register (sspsr) msb lsb sdo sdi processor 1 sck spi master sspm3:sspm0 = 00xxb serial input buffer (sspbuf) shift register (sspsr) lsb msb sdi sdo processor 2 sck spi slave sspm3:sspm0 = 010xb serial clock
pic18fxx39 ds30485a-page 130 preliminary ? 2002 microchip technology inc. 16.3.5 master mode the master can initiate the data transfer at any time because it controls the sck. the master determines when the slave (processor 2, figure 16-2) is to broadcast data by the software protocol. in master mode, the data is transmitted/received as soon as the sspbuf register is written to. if the spi is only going to receive, the sdo output could be dis- abled (programmed as an input). the sspsr register will continue to shift in the signal present on the sdi pin at the programmed clock rate. as each byte is received, it will be loaded into the sspbuf register as if a normal received byte (interrupts and status bits appropriately set). this could be useful in receiver applications as a ?line activity monitor? mode. the clock polarity is selected by appropriately program- ming the ckp bit (sspcon1<4>). this then, would give waveforms for spi communication as shown in figure 16-3, figure 16-5, and figure 16-6, where the msb is transmitted first. in master mode, the spi clock rate (bit rate) is user-programmable to be one of the following: ?f osc /4 (or t cy ) ?f osc /16 (or 4 ? t cy ) ?f osc /64 (or 16 ? t cy ) this allows a maximum data rate (at 40 mhz) of 10.00 mbps. figure 16-3 shows the waveforms for master mode. when the cke bit is set, the sdo data is valid before there is a clock edge on sck. the change of the input sample is shown based on the state of the smp bit. the time when the sspbuf is loaded with the received data is shown. figure 16-3: spi mode waveform (master mode) sck (ckp = 0 sck (ckp = 1 sck (ckp = 0 sck (ckp = 1 4 clock modes input sample input sample sdi bit7 bit0 sdo bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 bit7 bit0 sdi sspif (smp = 1) (smp = 0) (smp = 1) cke = 1) cke = 0) cke = 1) cke = 0) (smp = 0) write to sspbuf sspsr to sspbuf sdo bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 (cke = 0) (cke = 1) next q4 cycle after q2
? 2002 microchip technology inc. preliminary ds30485a-page 131 pic18fxx39 16.3.6 slave mode in slave mode, the data is transmitted and received as the external clock pulses appear on sck. when the last bit is latched, the sspif interrupt flag bit is set. while in slave mode, the external clock is supplied by the external clock source on the sck pin. this external clock must meet the minimum high and low times, as specified in the electrical specifications. while in sleep mode, the slave can transmit/receive data. when a byte is received, the device will wake-up from sleep. 16.3.7 slave select synchronization the ss pin allows a synchronous slave mode. the spi must be in slave mode with ss pin control enabled (sspcon1<3:0> = 04h). the pin must not be driven low for the ss pin to function as an input. the data latch must be high. when the ss pin is low, transmis- sion and reception are enabled and the sdo pin is driven. when the ss pin goes high, the sdo pin is no longer driven, even if in the middle of a transmitted byte, and becomes a floating output. external pull-up/ pull-down resistors may be desirable, depending on the application. when the spi module resets, the bit counter is forced to ?0?. this can be done by either forcing the ss pin to a high level or clearing the sspen bit. to emulate two-wire communication, the sdo pin can be connected to the sdi pin. when the spi needs to operate as a receiver, the sdo pin can be configured as an input. this disables transmissions from the sdo. the sdi can always be left as an input (sdi function), since it cannot create a bus conflict. figure 16-4: slave synchronization waveform note 1: when the spi is in slave mode with ss pin control enabled (sspcon<3:0> = 0100 ), the spi module will reset if the ss pin is set to v dd . 2: if the spi is used in slave mode with cke set, then the ss pin control must be enabled. sck (ckp = 1 sck (ckp = 0 input sample sdi bit7 sdo bit7 bit6 bit7 sspif interrupt (smp = 0) cke = 0) cke = 0) (smp = 0) write to sspbuf sspsr to sspbuf ss flag bit0 bit7 bit0 next q4 cycle after q2
pic18fxx39 ds30485a-page 132 preliminary ? 2002 microchip technology inc. figure 16-5: spi mode waveform (slave mode with cke = 0) figure 16-6: spi mode waveform (slave mode with cke = 1) sck (ckp = 1 sck (ckp = 0 input sample sdi bit7 bit0 sdo bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sspif interrupt (smp = 0) cke = 0) cke = 0) (smp = 0) write to sspbuf sspsr to sspbuf ss flag optional next q4 cycle after q2 sck (ckp = 1 sck (ckp = 0 input sample sdi bit7 bit0 sdo bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sspif interrupt (smp = 0) cke = 1) cke = 1) (smp = 0) write to sspbuf sspsr to sspbuf ss flag not optional next q4 cycle after q2
? 2002 microchip technology inc. preliminary ds30485a-page 133 pic18fxx39 16.3.8 sleep operation in master mode, all module clocks are halted and the transmission/reception will remain in that state until the device wakes from sleep. after the device returns to normal mode, the module will continue to transmit/ receive data. in slave mode, the spi transmit/receive shift register operates asynchronously to the device. this allows the device to be placed in sleep mode and data to be shifted into the spi transmit/receive shift register. when all 8 bits have been received, the mssp interrupt flag bit will be set and if enabled, will wake the device from sleep. 16.3.9 effects of a reset a reset disables the mssp module and terminates the current transfer. 16.3.10 bus mode compatibility table 16-1 shows the compatibility between the standard spi modes and the states the ckp and cke control bits. table 16-1: spi bus modes there is also an smp bit which controls when the data is sampled. table 16-2: registers associated with spi operation standard spi mode terminology control bits state ckp cke 0, 0 0 1 0, 1 0 0 1, 0 1 1 1, 1 1 0 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 trisc trisc7 trisc6 trisc5 trisc4 trisc3 * * trisc0 1111 1111 1111 1111 sspbuf synchronous serial port receive buffer/transmit register xxxx xxxx uuuu uuuu sspcon wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 0000 0000 trisa ? porta data direction register -111 1111 -111 1111 sspstat smp cke d/a p s r/w ua bf 0000 0000 0000 0000 legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used by the mssp in spi mode. * reserved bits; do not modify. note 1: the pspif, pspie and pspip bits are reserved on the pic18c2x2 devices; always maintain these bits clear.
pic18fxx39 ds30485a-page 134 preliminary ? 2002 microchip technology inc. 16.4 i 2 c mode the mssp module in i 2 c mode fully implements all master and slave functions (including general call sup- port) and provides interrupts on start and stop bits in hardware to determine a free bus (multi-master func- tion). the mssp module implements the standard mode specifications, as well as 7-bit and 10-bit addressing. two pins are used for data transfer: ? serial clock (scl) - rc3/sck/scl ? serial data (sda) - rc4/sdi/sda the user must configure these pins as inputs or outputs through the trisc<4:3> bits. figure 16-7: mssp block diagram (i 2 c mode) 16.4.1 registers the mssp module has six registers for i 2 c operation. these are: ? mssp control register1 (sspcon1) ? mssp control register2 (sspcon2) ? mssp status register (sspstat) ? serial receive/transmit buffer (sspbuf) ? mssp shift register (sspsr) - not directly accessible ? mssp address register (sspadd) sspcon, sspcon2 and sspstat are the control and status registers in i 2 c mode operation. the sspcon and sspcon2 registers are readable and writable. the lower 6 bits of the sspstat are read only. the upper two bits of the sspstat are read/write. sspsr is the shift register used for shifting data in or out. sspbuf is the buffer register to which data bytes are written to or read from. sspadd register holds the slave device address when the ssp is configured in i 2 c slave mode. when the ssp is configured in master mode, the lower seven bits of sspadd act as the baud rate generator reload value. in receive operations, sspsr and sspbuf together, create a double-buffered receiver. when sspsr receives a complete byte, it is transferred to sspbuf and the sspif interrupt is set. during transmission, the sspbuf is not double- buffered. a write to sspbuf will write to both sspbuf and sspsr. read write sspsr reg match detect sspadd reg start and stop bit detect sspbuf reg internal data bus addr match set, reset s, p bits (sspstat reg) rc3/sck/scl rc4/ shift clock msb sdi/ lsb sda
? 2002 microchip technology inc. preliminary ds30485a-page 135 pic18fxx39 register 16-3: sspstat: mssp status register (i 2 c mode) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a psr/w ua bf bit 7 bit 0 bit 7 smp: slew rate control bit in master or slave mode: 1 = slew rate control disabled for standard speed mode (100 khz and 1 mhz) 0 = slew rate control enabled for high speed mode (400 khz) bit 6 cke: smbus select bit in master or slave mode: 1 = enable smbus specific inputs 0 = disable smbus specific inputs bit 5 d/a : data/address bit in master mode: reserved in slave mode: 1 = indicates that the last byte received or transmitted was data 0 = indicates that the last byte received or transmitted was address bit 4 p: stop bit 1 = indicates that a stop bit has been detected last 0 = stop bit was not detected last note: this bit is cleared on reset and when sspen is cleared. bit 3 s: start bit 1 = indicates that a start bit has been detected last 0 = start bit was not detected last note: this bit is cleared on reset and when sspen is cleared. bit 2 r/w : read/write bit information (i 2 c mode only) in slave mode: 1 = read 0 = write note: this bit holds the r/w bit information following the last address match. this bit is only valid from the address match to the next start bit, stop bit, or not ack bit. in master mode: 1 = transmit is in progress 0 = transmit is not in progress note: oring this bit with sen, rsen, pen, rcen, or acken will indicate if the mssp is in idle mode. bit 1 ua: update address (10-bit slave mode only) 1 = indicates that the user needs to update the address in the sspadd register 0 = address does not need to be updated bit 0 bf: buffer full status bit in transmit mode: 1 = receive complete, sspbuf is full 0 = receive not complete, sspbuf is empty in receive mode: 1 = data transmit in progress (does not include the ack and stop bits), sspbuf is full 0 = data transmit complete (does not include the ack and stop bits), sspbuf is empty legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 136 preliminary ? 2002 microchip technology inc. register 16-4: sspcon1: mssp control register 1 (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 bit 7 wcol: write collision detect bit in master transmit mode: 1 = a write to the sspbuf register was attempted while the i 2 c conditions were not valid for a transmission to be started (must be cleared in software) 0 = no collision in slave transmit mode: 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared in software) 0 = no collision in receive mode (master or slave modes): this is a ?don?t care? bit bit 6 sspov: receive overflow indicator bit in receive mode: 1 = a byte is received while the sspbuf register is still holding the previous byte (must be cleared in software) 0 = no overflow in transmit mode: this is a ?don?t care? bit in transmit mode bit 5 sspen: synchronous serial port enable bit 1 = enables the serial port and configures the sda and scl pins as the serial port pins 0 = disables serial port and configures these pins as i/o port pins note: when enabled, the sda and scl pins must be properly configured as input or output. bit 4 ckp: sck release control bit in slave mode: 1 = release clock 0 = holds clock low (clock stretch), used to ensure data setup time in master mode: unused in this mode bit 3-0 sspm3:sspm0: synchronous serial port mode select bits 1111 = i 2 c slave mode, 10-bit address with start and stop bit interrupts enabled 1110 = i 2 c slave mode, 7-bit address with start and stop bit interrupts enabled 1011 = i 2 c firmware controlled master mode (slave idle) 1000 = i 2 c master mode, clock = f osc / (4 * (sspadd+1)) 0111 = i 2 c slave mode, 10-bit address 0110 = i 2 c slave mode, 7-bit address note: bit combinations not specifically listed here are either reserved, or implemented in spi mode only. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 137 pic18fxx39 register 16-5: sspcon2: mssp control register 2 (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 gcen ackstat ackdt acken rcen pen rsen sen bit 7 bit 0 bit 7 gcen: general call enable bit (slave mode only) 1 = enable interrupt when a general call address (0000h) is received in the sspsr 0 = general call address disabled bit 6 ackstat: acknowledge status bit (master transmit mode only) 1 = acknowledge was not received from slave 0 = acknowledge was received from slave bit 5 ackdt: acknowledge data bit (master receive mode only) 1 = not acknowledge 0 = acknowledge note: value that will be transmitted when the user initiates an acknowledge sequence at the end of a receive. bit 4 acken: acknowledge sequence enable bit (master receive mode only) 1 = initiate acknowledge sequence on sda and scl pins, and transmit ackdt data bit. automatically cleared by hardware. 0 = acknowledge sequence idle bit 3 rcen: receive enable bit (master mode only) 1 = enables receive mode for i 2 c 0 = receive idle bit 2 pen: stop condition enable bit (master mode only) 1 = initiate stop condition on sda and scl pins. automatically cleared by hardware. 0 = stop condition idle bit 1 rsen: repeated start condition enabled bit (master mode only) 1 = initiate repeated start condition on sda and scl pins. automatically cleared by hardware. 0 = repeated start condition idle bit 0 sen: start condition enabled/stretch enabled bit in master mode: 1 = initiate start condition on sda and scl pins. automatically cleared by hardware. 0 = start condition idle in slave mode: 1 = clock stretching is enabled for both slave transmit and slave receive (stretch enabled) 0 = clock stretching is enabled for slave transmit only (legacy mode) note: for bits acken, rcen, pen, rsen, sen: if the i 2 c module is not in the idle mode, this bit may not be set (no spooling) and the sspbuf may not be written (or writes to the sspbuf are disabled). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 138 preliminary ? 2002 microchip technology inc. 16.4.2 operation the mssp module functions are enabled by setting mssp enable bit, sspen (sspcon<5>). the sspcon1 register allows control of the i 2 c oper- ation. four mode selection bits (sspcon<3:0>) allow one of the following i 2 c modes to be selected: ?i 2 c master mode, clock = osc/4 (sspadd +1) ?i 2 c slave mode (7-bit address) ?i 2 c slave mode (10-bit address) ?i 2 c slave mode (7-bit address), with start and stop bit interrupts enabled ?i 2 c slave mode (10-bit address), with start and stop bit interrupts enabled ?i 2 c firmware controlled master operation, slave is idle selection of any i 2 c mode, with the sspen bit set, forces the scl and sda pins to be open drain, pro- vided these pins are programmed to inputs by setting the appropriate trisc bits. to ensure proper operation of the module, pull-up resistors must be provided externally to the scl and sda pins. 16.4.3 slave mode in slave mode, the scl and sda pins must be config- ured as inputs (trisc<4:3> set). the mssp module will override the input state with the output data when required (slave-transmitter). the i 2 c slave mode hardware will always generate an interrupt on an address match. through the mode select bits, the user can also choose to interrupt on start and stop bits when an address is matched, or the data transfer after an address match is received, the hardware automati- cally will generate the acknowledge (ack ) pulse and load the sspbuf register with the received value currently in the sspsr register. any combination of the following conditions will cause the mssp module not to give this ack pulse: ? the buffer full bit bf (sspstat<0>) was set before the transfer was received. ? the overflow bit sspov (sspcon<6>) was set before the transfer was received. in this case, the sspsr register value is not loaded into the sspbuf, but bit sspif (pir1<3>) is set. the bf bit is cleared by reading the sspbuf register, while bit sspov is cleared through software. the scl clock input must have a minimum high and low for proper operation. the high and low times of the i 2 c specification, as well as the requirement of the mssp module, are shown in timing parameter 100 and parameter 101. 16.4.3.1 addressing once the mssp module has been enabled, it waits for a start condition to occur. following the start con- dition, the 8-bits are shifted into the sspsr register. all incoming bits are sampled with the rising edge of the clock (scl) line. the value of register sspsr<7:1> is compared to the value of the sspadd register. the address is compared on the falling edge of the eighth clock (scl) pulse. if the addresses match, and the bf and sspov bits are clear, the following events occur: 1. the sspsr register value is loaded into the sspbuf register. 2. the buffer full bit bf is set. 3. an ack pulse is generated. 4. mssp interrupt flag bit, sspif (pir1<3>) is set (interrupt is generated if enabled) on the falling edge of the ninth scl pulse. in 10-bit address mode, two address bytes need to be received by the slave. the five most significant bits (msbs) of the first address byte specify if this is a 10-bit address. bit r/w (sspstat<2>) must specify a write so the slave device will receive the second address byte. for a 10-bit address, the first byte would equal ? 11110 a9 a8 0 ?, where ? a9 ? and ? a8 ? are the two msbs of the address. the sequence of events for 10-bit address is as follows, with steps 7 through 9 for the slave-transmitter: 1. receive first (high) byte of address (bits sspif, bf and bit ua (sspstat<1>) are set). 2. update the sspadd register with second (low) byte of address (clears bit ua and releases the scl line). 3. read the sspbuf register (clears bit bf) and clear flag bit sspif. 4. receive second (low) byte of address (bits sspif, bf, and ua are set). 5. update the sspadd register with the first (high) byte of address. if match releases scl line, this will clear bit ua. 6. read the sspbuf register (clears bit bf) and clear flag bit sspif. 7. receive repeated start condition. 8. receive first (high) byte of address (bits sspif and bf are set). 9. read the sspbuf register (clears bit bf) and clear flag bit sspif.
? 2002 microchip technology inc. preliminary ds30485a-page 139 pic18fxx39 16.4.3.2 reception when the r/w bit of the address byte is clear and an address match occurs, the r/w bit of the sspstat register is cleared. the received address is loaded into the sspbuf register and the sda line is held low (ack ). when the address byte overflow condition exists, then the no acknowledge (ack ) pulse is given. an overflow condition is defined as either bit bf (sspstat<0>) is set, or bit sspov (sspcon1<6>) is set. an mssp interrupt is generated for each data transfer byte. flag bit sspif (pir1<3>) must be cleared in soft- ware. the sspstat register is used to determine the status of the byte. if sen is enabled (sspcon1<0> = 1), rc3/sck/scl will be held low (clock stretch) following each data trans- fer. the clock must be released by setting bit ckp (sspcon<4>). see section 16.4.4 (?clock stretching?), for more detail. 16.4.3.3 transmission when the r/w bit of the incoming address byte is set and an address match occurs, the r/w bit of the sspstat register is set. the received address is loaded into the sspbuf register. the ack pulse will be sent on the ninth bit and pin rc3/sck/scl is held low, regardless of sen (see ?clock stretching?, section 16.4.4, for more detail). by stretching the clock, the master will be unable to assert another clock pulse until the slave is done preparing the transmit data. the transmit data must be loaded into the sspbuf register, which also loads the sspsr register. then, pin rc3/ sck/scl should be enabled by setting bit ckp (sspcon1<4>). the eight data bits are shifted out on the falling edge of the scl input. this ensures that the sda signal is valid during the scl high time (figure 16-9). the ack pulse from the master-receiver is latched on the rising edge of the ninth scl input pulse. if the sda line is high (not ack ), then the data transfer is com- plete. in this case, when the ack is latched by the slave, the slave logic is reset (resets sspstat regis- ter) and the slave monitors for another occurrence of the start bit. if the sda line was low (ack ), the next transmit data must be loaded into the sspbuf register. again, pin rc3/sck/scl must be enabled by setting bit ckp. an mssp interrupt is generated for each data transfer byte. the sspif bit must be cleared in software and the sspstat register is used to determine the status of the byte. the sspif bit is set on the falling edge of the ninth clock pulse.
pic18fxx39 ds30485a-page 140 preliminary ? 2002 microchip technology inc. figure 16-8: i 2 c slave mode timing with sen = 0 (reception, 7-bit address) sda scl sspif bf (sspstat<0>) sspov (sspcon<6>) s 1 2 34 56 7 8 91 234 5 67 89 1 23 45 7 89 p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d1 d0 ack receiving data ack receiving data r/w = 0 ack receiving address cleared in software sspbuf is read bus master terminates transfer sspov is set because sspbuf is still full. ack is not sent. d2 6 (pir1<3>) ckp (ckp does not reset to ?0? when sen = 0)
? 2002 microchip technology inc. preliminary ds30485a-page 141 pic18fxx39 figure 16-9: i 2 c slave mode timing (transmission, 7-bit address) sda scl sspif (pir1<3>) bf (sspstat<0>) a6 a5 a4 a3 a2 a1 d6 d5 d4 d3 d2 d1 d0 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 9 sspbuf is written in software cleared in software scl held low while cpu responds to sspif from sspif isr data in sampled s ack transmitting data r/w = 1 ack receiving address a7 d7 9 1 d6 d5 d4 d3 d2 d1 d0 2 3 4 5 6 7 8 9 sspbuf is written in software cleared in software from sspif isr transmitting data d7 1 ckp p ack ckp is set in software ckp is set in software
pic18fxx39 ds30485a-page 142 preliminary ? 2002 microchip technology inc. figure 16-10: i 2 c slave mode timing with sen = 0 (reception, 10-bit address) sda scl sspif bf (sspstat<0>) s 123456789 123456789 12345 789 p 1 1 1 1 0 a9a8 a7 a6a5a4a3a2a1 a0 d7 d6d5d4d3 d1d0 receive data byte ack r/w = 0 ack receive first byte of address cleared in software d2 6 (pir1<3>) cleared in software receive second byte of address cleared by hardware when sspadd is updated with low byte of address ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag ack ckp 12345 789 d7 d6 d5 d4 d3 d1 d0 receive data byte bus master terminates transfer d2 6 ack cleared in software cleared in software sspov (sspcon<6>) sspov is set because sspbuf is still full. ack is not sent. (ckp does not reset to ?0? when sen = 0) clock is held low until update of sspadd has taken place
? 2002 microchip technology inc. preliminary ds30485a-page 143 pic18fxx39 figure 16-11: i 2 c slave mode timing (transmission, 10-bit address) sda scl sspif bf (sspstat<0>) s 1 234 56 7 89 1 2345 67 89 1 2345 7 89 p 11110a9a8 a7 a6a5a4a3a2a1a0 11110 a8 r/w=1 ack ack r/w = 0 ack receive first byte of address cleared in software bus master terminates transfer a9 6 (pir1<3>) receive second byte of address cleared by hardware when sspadd is updated with low byte of address ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag receive first byte of address 12345 789 d7 d6 d5 d4 d3 d1 ack d2 6 transmitting data byte d0 dummy read of sspbuf to clear bf flag sr cleared in software write of sspbuf initiates transmit cleared in software completion of clears bf flag ckp (sspcon<4>) ckp is set in software ckp is automatically cleared in hardware holding scl low clock is held low until update of sspadd has taken place data transmission clock is held low until ckp is set to ?1? bf flag is clear third address sequence at the end of the
pic18fxx39 ds30485a-page 144 preliminary ? 2002 microchip technology inc. 16.4.4 clock stretching both 7- and 10-bit slave modes implement automatic clock stretching during a transmit sequence. the sen bit (sspcon2<0>) allows clock stretching to be enabled during receives. setting sen will cause the scl pin to be held low at the end of each data receive sequence. 16.4.4.1 clock stretching for 7-bit slave receive mode (sen = 1) in 7-bit slave receive mode, on the falling edge of the ninth clock at the end of the ack sequence, if the bf bit is set, the ckp bit in the sspcon1 register is auto- matically cleared, forcing the scl output to be held low. the ckp being cleared to ?0? will assert the scl line low. the ckp bit must be set in the user?s isr before reception is allowed to continue. by holding the scl line low, the user has time to service the isr and read the contents of the sspbuf before the master device can initiate another receive sequence. this will prevent buffer overruns from occurring (see figure 16-13). 16.4.4.2 clock stretching for 10-bit slave receive mode (sen = 1) in 10-bit slave receive mode, during the address sequence, clock stretching automatically takes place but ckp is not cleared. during this time, if the ua bit is set after the ninth clock, clock stretching is initiated. the ua bit is set after receiving the upper byte of the 10-bit address, and following the receive of the second byte of the 10-bit address with the r/w bit cleared to ?0?. the release of the clock line occurs upon updating sspadd. clock stretching will occur on each data receive sequence, as described in 7-bit mode. 16.4.4.3 clock stretching for 7-bit slave transmit mode 7-bit slave transmit mode implements clock stretching by clearing the ckp bit after the falling edge of the ninth clock, if the bf bit is clear. this occurs, regardless of the state of the sen bit. the user?s isr must set the ckp bit before transmis- sion is allowed to continue. by holding the scl line low, the user has time to service the isr and load the contents of the sspbuf before the master device can initiate another transmit sequence (see figure 16-9). 16.4.4.4 clock stretching for 10-bit slave transmit mode in 10-bit slave transmit mode, clock stretching is con- trolled during the first two address sequences by the state of the ua bit, just as it is in 10-bit slave receive mode. the first two addresses are followed by a third address sequence, which contains the high order bits of the 10-bit address and the r/w bit set to ?1?. after the third address sequence is performed, the ua bit is not set, the module is now configured in transmit mode, and clock stretching is controlled by the bf flag, as in 7-bit slave transmit mode (see figure 16-11). note 1: if the user reads the contents of the sspbuf before the falling edge of the ninth clock, thus clearing the bf bit, the ckp bit will not be cleared and clock stretching will not occur. 2: the ckp bit can be set in software, regardless of the state of the bf bit. the user should be careful to clear the bf bit in the isr before the next receive sequence, in order to prevent an overflow condition. note: if the user polls the ua bit and clears it by updating the sspadd register before the falling edge of the ninth clock occurs, and if the user hasn?t cleared the bf bit by read- ing the sspbuf register before that time, then the ckp bit will still not be asserted low. clock stretching on the basis of the state of the bf bit only occurs during a data sequence, not an address sequence. note 1: if the user loads the contents of sspbuf, setting the bf bit before the falling edge of the ninth clock, the ckp bit will not be cleared and clock stretching will not occur. 2: the ckp bit can be set in software, regardless of the state of the bf bit.
? 2002 microchip technology inc. preliminary ds30485a-page 145 pic18fxx39 16.4.4.5 clock synchronization and the ckp bit if a user clears the ckp bit, the scl output is forced to ?0?. setting the ckp bit will not assert the scl output low until the scl output is already sampled low. if the user attempts to drive scl low, the ckp bit will not assert the scl line until an external i 2 c master device has already asserted the scl line. the scl output will remain low until the ckp bit is set, and all other devices on the i 2 c bus have de-asserted scl. this ensures that a write to the ckp bit will not violate the minimum high time requirement for scl (see figure 16-12). figure 16-12: clock synchronization timing sda scl dx-1 dx wr q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 sspcon ckp master device de-asserts clock master device asserts clock
pic18fxx39 ds30485a-page 146 preliminary ? 2002 microchip technology inc. figure 16-13: i 2 c slave mode timing with sen = 1 (reception, 7-bit address) sda scl sspif bf (sspstat<0>) sspov (sspcon<6>) s 1 234 56 7 8 9 1 2345 67 89 1 23 45 7 89 p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d1 d0 ack receiving data ack receiving data r/w = 0 ack receiving address cleared in software sspbuf is read bus master terminates transfer sspov is set because sspbuf is still full. ack is not sent. d2 6 (pir1<3>) ckp ckp written to ?1? in if bf is cleared prior to the falling edge of the 9th clock, ckp will not be reset to ?0? and no clock stretching will occur software clock is held low until ckp is set to ?1? clock is not held low because buffer full bit is clear prior to falling edge of 9th clock clock is not held low because ack = 1 bf is set after falling edge of the 9th clock, ckp is reset to ?0? and clock stretching occurs
? 2002 microchip technology inc. preliminary ds30485a-page 147 pic18fxx39 figure 16-14: i 2 c slave mode timing with sen = 1 (reception, 10-bit address) sda scl sspif bf (sspstat<0>) s 1 234 56 7 89 1 2345 67 89 1 2345 7 89 p 1 1 1 1 0 a9a8 a7 a6a5a4a3a2a1 a0 d7d6d5d4d3 d1d0 receive data byte ack r/w = 0 ack receive first byte of address cleared in software d2 6 (pir1<3>) cleared in software receive second byte of address cleared by hardware when sspadd is updated with low byte of address after falling edge ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address after falling edge sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag ack ckp 12345 789 d7 d6 d5 d4 d3 d1 d0 receive data byte bus master terminates transfer d2 6 ack cleared in software cleared in software sspov (sspcon<6>) ckp written to ?1? note: an update of the sspadd register before the falling edge of the ninth clock will have no effect on ua, and ua will remain set. note: an update of the sspadd register before the falling edge of the ninth clock will have no effect on ua, and ua will remain set. in software clock is held low until update of sspadd has taken place of ninth clock of ninth clock sspov is set because sspbuf is still full. ack is not sent. dummy read of sspbuf to clear bf flag clock is held low until ckp is set to ?1? clock is not held low because ack = 1
pic18fxx39 ds30485a-page 148 preliminary ? 2002 microchip technology inc. 16.4.5 general call address support the addressing procedure for the i 2 c bus is such that, the first byte after the start condition usually deter- mines which device will be the slave addressed by the master. the exception is the general call address, which can address all devices. when this address is used, all devices should, in theory, respond with an acknowledge. the general call address is one of eight addresses reserved for specific purposes by the i 2 c protocol. it consists of all ?0?s with r/w = 0. the general call address is recognized when the gen- eral call enable bit (gcen) is enabled (sspcon2<7> set). following a start bit detect, 8-bits are shifted into the sspsr and the address is compared against the sspadd. it is also compared to the general call address and fixed in hardware. if the general call address matches, the sspsr is transferred to the sspbuf, the bf flag bit is set (eighth bit), and on the falling edge of the ninth bit (ack bit), the sspif interrupt flag bit is set. when the interrupt is serviced, the source for the inter- rupt can be checked by reading the contents of the sspbuf. the value can be used to determine if the address was device specific or a general call address. in 10-bit mode, the sspadd is required to be updated for the second half of the address to match, and the ua bit is set (sspstat<1>). if the general call address is sampled when the gcen bit is set, while the slave is configured in 10-bit address mode, then the second half of the address is not necessary, the ua bit will not be set, and the slave will begin receiving data after the acknowledge (figure 16-15). figure 16-15: slave mode general call address sequence (7 or 10-bit address mode) sda scl s sspif bf (sspstat<0>) sspov (sspcon1<6>) cleared in software sspbuf is read r/w = 0 ack general call address address is compared to general call address gcen (sspcon2<7>) receiving data ack 123456789123456789 d7 d6 d5 d4 d3 d2 d1 d0 after ack, set interrupt '0' '1'
? 2002 microchip technology inc. preliminary ds30485a-page 149 pic18fxx39 16.4.6 master mode master mode is enabled by setting and clearing the appropriate sspm bits in sspcon1 and by setting the sspen bit. in master mode, the scl and sda lines are manipulated by the mssp hardware. master mode of operation is supported by interrupt generation on the detection of the start and stop conditions. the stop (p) and start (s) bits are cleared from a reset or when the mssp module is disabled. control of the i 2 c bus may be taken when the p bit is set or the bus is idle, with both the s and p bits clear. in firmware controlled master mode, user code con- ducts all i 2 c bus operations based on start and stop bit conditions. once master mode is enabled, the user has six options. 1. assert a start condition on sda and scl. 2. assert a repeated start condition on sda and scl. 3. write to the sspbuf register initiating transmission of data/address. 4. configure the i 2 c port to receive data. 5. generate an acknowledge condition at the end of a received byte of data. 6. generate a stop condition on sda and scl. the following events will cause ssp interrupt flag bit, sspif, to be set (ssp interrupt if enabled): ? start condition ? stop condition ? data transfer byte transmitted/received ? acknowledge transmit ? repeated start figure 16-16: mssp block diagram (i 2 c master mode) note: the mssp module, when configured in i 2 c master mode, does not allow queueing of events. for instance, the user is not allowed to initiate a start condition and immediately write the sspbuf register to initiate transmission before the start condition is complete. in this case, the sspbuf will not be written to and the wcol bit will be set, indicating that a write to the sspbuf did not occur. read write sspsr start bit, stop bit, start bit detect sspbuf internal data bus set/reset, s, p, wcol (sspstat) shift clock msb lsb sda acknowledge generate stop bit detect write collision detect clock arbitration state counter for end of xmit/rcv scl scl in bus collision sda in receive enable clock cntl clock arbitrate/wcol detect (hold off clock source) sspadd<6:0> baud set sspif, bclif reset ackstat, pen (sspcon2) rate generator sspm3:sspm0
pic18fxx39 ds30485a-page 150 preliminary ? 2002 microchip technology inc. 16.4.6.1 i 2 c master mode operation the master device generates all of the serial clock pulses and the start and stop conditions. a trans- fer is ended with a stop condition, or with a repeated start condition. since the repeated start condi- tion is also the beginning of the next serial transfer, the i 2 c bus will not be released. in master transmitter mode, serial data is output through sda, while scl outputs the serial clock. the first byte transmitted contains the slave address of the receiving device (7 bits) and the read/write (r/w ) bit. in this case, the r/w bit will be logic '0'. serial data is transmitted 8 bits at a time. after each byte is transmit- ted, an acknowledge bit is received. start and stop conditions are output to indicate the beginning and the end of a serial transfer. in master receive mode, the first byte transmitted con- tains the slave address of the transmitting device (7 bits) and the r/w bit. in this case, the r/w bit will be logic '1'. thus, the first byte transmitted is a 7-bit slave address followed by a '1' to indicate the receive bit. serial data is received via sda, while scl outputs the serial clock. serial data is received 8 bits at a time. after each byte is received, an acknowledge bit is transmit- ted. start and stop conditions indicate the beginning and end of transmission. the baud rate generator used for the spi mode opera- tion is used to set the scl clock frequency for either 100 khz, 400 khz or 1 mhz i 2 c operation. see section 16.4.7 (?baud rate generator?), for more detail. a typical transmit sequence would go as follows: 1. the user generates a start condition by set- ting the start enable bit, sen (sspcon2<0>). 2. sspif is set. the mssp module will wait the required start time before any other operation takes place. 3. the user loads the sspbuf with the slave address to transmit. 4. address is shifted out the sda pin until all 8 bits are transmitted. 5. the mssp module shifts in the ack bit from the slave device and writes its value into the sspcon2 register (sspcon2<6>). 6. the mssp module generates an interrupt at the end of the ninth clock cycle by setting the sspif bit. 7. the user loads the sspbuf with eight bits of data. 8. data is shifted out the sda pin until all 8 bits are transmitted. 9. the mssp module shifts in the ack bit from the slave device and writes its value into the sspcon2 register (sspcon2<6>). 10. the mssp module generates an interrupt at the end of the ninth clock cycle by setting the sspif bit. 11. the user generates a stop condition by setting the stop enable bit pen (sspcon2<2>). 12. interrupt is generated once the stop condition is complete.
? 2002 microchip technology inc. preliminary ds30485a-page 151 pic18fxx39 16.4.7 baud rate generator in i 2 c master mode, the baud rate generator (brg) reload value is placed in the lower 7 bits of the sspadd register (figure 16-17). when a write occurs to sspbuf, the baud rate generator will automatically begin counting. the brg counts down to ?0? and stops until another reload has taken place. the brg count is decremented twice per instruction cycle (t cy ) on the q2 and q4 clocks. in i 2 c master mode, the brg is reloaded automatically. once the given operation is complete (i.e., transmis- sion of the last data bit is followed by ack ), the internal clock will automatically stop counting and the scl pin will remain in its last state. table 15-3 demonstrates clock rates based on instruction cycles and the brg value loaded into sspadd. figure 16-17: baud rate generator block diagram table 16-3: i 2 c clock rate w/brg sspm3:sspm0 brg down counter clko f osc /4 sspadd<6:0> sspm3:sspm0 scl reload control reload f cy f cy *2 brg value f scl (2) (2 rollovers of brg) 10 mhz 20 mhz 19h 400 khz (1) 10 mhz 20 mhz 20h 312.5 khz 10 mhz 20 mhz 3fh 100 khz 4 mhz 8 mhz 0ah 400 khz (1) 4 mhz 8 mhz 0dh 308 khz 4 mhz 8 mhz 28h 100 khz 1 mhz 2 mhz 03h 333 khz (1) 1 mhz 2 mhz 0ah 100khz 1 mhz 2 mhz 00h 1 mhz (1) note 1: the i 2 c interface does not conform to the 400 khz i 2 c specification (which applies to rates greater than 100 khz) in all details, but may be used with care where higher rates are required by the application. 2: actual frequency will depend on bus conditions. theoretically, bus conditions will add rise time and extend low time of clock period, producing the effective frequency.
pic18fxx39 ds30485a-page 152 preliminary ? 2002 microchip technology inc. 16.4.7.1 clock arbitration clock arbitration occurs when the master, during any receive, transmit or repeated start/stop condition, de-asserts the scl pin (scl allowed to float high). when the scl pin is allowed to float high, the baud rate generator (brg) is suspended from counting until the scl pin is actually sampled high. when the scl pin is sampled high, the baud rate generator is reloaded with the contents of sspadd<6:0> and begins counting. this ensures that the scl high time will always be at least one brg rollover count, in the event that the clock is held low by an external device (figure 16-18). figure 16-18: baud rate generator timing with clock arbitration sda scl scl de-asserted but slave holds dx-1 dx brg scl is sampled high, reload takes place and brg starts its count 03h 02h 01h 00h (hold off) 03h 02h reload brg value scl low (clock arbitration) scl allowed to transition high brg decrements on q2 and q4 cycles
? 2002 microchip technology inc. preliminary ds30485a-page 153 pic18fxx39 16.4.8 i 2 c master mode start condition timing to initiate a start condition, the user sets the start condition enable bit, sen (sspcon2<0>). if the sda and scl pins are sampled high, the baud rate genera- tor is reloaded with the contents of sspadd<6:0> and starts its count. if scl and sda are both sampled high when the baud rate generator times out (t brg ), the sda pin is driven low. the action of the sda being driven low, while scl is high, is the start condition and causes the s bit (sspstat<3>) to be set. follow- ing this, the baud rate generator is reloaded with the contents of sspadd<6:0> and resumes its count. when the baud rate generator times out (t brg ), the sen bit (sspcon2<0>) will be automatically cleared by hardware, the baud rate generator is suspended, leaving the sda line held low and the start condition is complete. 16.4.8.1 wcol status flag if the user writes the sspbuf when a start sequence is in progress, the wcol is set and the con- tents of the buffer are unchanged (the write doesn?t occur). figure 16-19: first start bit timing note: if at the beginning of the start condition, the sda and scl pins are already sam- pled low, or if during the start condition the scl line is sampled low before the sda line is driven low, a bus collision occurs, the bus collision interrupt flag, bclif is set, the start condition is aborted, and the i 2 c module is reset into its idle state. note: because queueing of events is not allowed, writing to the lower 5 bits of sspcon2 is disabled until the start condition is complete. sda scl s t brg 1st bit 2nd bit t brg sda = 1, at completion of start bit, scl = 1 write to sspbuf occurs here t brg hardware clears sen bit t brg write to sen bit occurs here set s bit (sspstat<3>) and sets sspif bit
pic18fxx39 ds30485a-page 154 preliminary ? 2002 microchip technology inc. 16.4.9 i 2 c master mode repeated start condition timing a repeated start condition occurs when the rsen bit (sspcon2<1>) is programmed high and the i 2 c logic module is in the idle state. when the rsen bit is set, the scl pin is asserted low. when the scl pin is sampled low, the baud rate generator is loaded with the contents of sspadd<5:0> and begins counting. the sda pin is released (brought high) for one baud rate generator count (t brg ). when the baud rate generator times out, if sda is sampled high, the scl pin will be de-asserted (brought high). when scl is sampled high, the baud rate generator is reloaded with the con- tents of sspadd<6:0> and begins counting. sda and scl must be sampled high for one t brg . this action is then followed by assertion of the sda pin (sda = 0) for one t brg , while scl is high. following this, the rsen bit (sspcon2<1>) will be automatically cleared and the baud rate generator will not be reloaded, leaving the sda pin held low. as soon as a start condition is detected on the sda and scl pins, the s bit (sspstat<3>) will be set. the sspif bit will not be set until the baud rate generator has timed out. immediately following the sspif bit getting set, the user may write the sspbuf with the 7-bit address in 7-bit mode, or the default first address in 10-bit mode. after the first eight bits are transmitted and an ack is received, the user may then transmit an additional eight bits of address (10-bit mode) or eight bits of data (7-bit mode). 16.4.9.1 wcol status flag if the user writes the sspbuf when a repeated start sequence is in progress, the wcol is set and the contents of the buffer are unchanged (the write doesn?t occur). figure 16-20: repeat start condition waveform note 1: if rsen is programmed while any other event is in progress, it will not take effect. 2: a bus collision during the repeated start condition occurs if: ? sda is sampled low when scl goes from low to high. ? scl goes low before sda is asserted low. this may indicate that another master is attempting to transmit a data "1". note: because queueing of events is not allowed, writing of the lower 5 bits of sspcon2 is disabled until the repeated start condition is complete. sda scl sr = repeated start write to sspcon2 write to sspbuf occurs here falling edge of ninth clock end of xmit at completion of start bit, hardware clears rsen bit 1st bit set s (sspstat<3>) t brg t brg sda = 1, sda = 1, scl (no change). scl = 1 occurs here. t brg t brg t brg and sets sspif
? 2002 microchip technology inc. preliminary ds30485a-page 155 pic18fxx39 16.4.10 i 2 c master mode transmission transmission of a data byte, a 7-bit address, or the other half of a 10-bit address is accomplished by simply writing a value to the sspbuf register. this action will set the buffer full flag bit, bf, and allow the baud rate generator to begin counting and start the next transmis- sion. each bit of address/data will be shifted out onto the sda pin after the falling edge of scl is asserted (see data hold time specification parameter 106). scl is held low for one baud rate generator rollover count (t brg ). data should be valid before scl is released high (see data setup time specification parameter 107). when the scl pin is released high, it is held that way for t brg . the data on the sda pin must remain stable for that duration and some hold time after the next fall- ing edge of scl. after the eighth bit is shifted out (the falling edge of the eighth clock), the bf flag is cleared and the master releases sda. this allows the slave device being addressed to respond with an ack bit during the ninth bit time, if an address match occurred, or if data was received properly. the status of ack is written into the ackdt bit on the falling edge of the ninth clock. if the master receives an acknowledge, the acknowledge status bit, ackstat, is cleared. if not, the bit is set. after the ninth clock, the sspif bit is set and the master clock (baud rate generator) is sus- pended until the next data byte is loaded into the sspbuf, leaving scl low and sda unchanged (figure 16-21). after the write to the sspbuf, each bit of address will be shifted out on the falling edge of scl until all seven address bits and the r/w bit are completed. on the fall- ing edge of the eighth clock, the master will de-assert the sda pin, allowing the slave to respond with an acknowledge. on the falling edge of the ninth clock, the master will sample the sda pin to see if the address was recognized by a slave. the status of the ack bit is loaded into the ackstat status bit (sspcon2<6>). following the falling edge of the ninth clock transmis- sion of the address, the sspif is set, the bf flag is cleared and the baud rate generator is turned off until another write to the sspbuf takes place, holding scl low and allowing sda to float. 16.4.10.1 bf status flag in transmit mode, the bf bit (sspstat<0>) is set when the cpu writes to sspbuf and is cleared when all 8 bits are shifted out. 16.4.10.2 wcol status flag if the user writes the sspbuf when a transmit is already in progress (i.e., sspsr is still shifting out a data byte), the wcol is set and the contents of the buffer are unchanged (the write doesn?t occur). wcol must be cleared in software. 16.4.10.3 ackstat status flag in transmit mode, the ackstat bit (sspcon2<6>) is cleared when the slave has sent an acknowledge (ack = 0), and is set when the slave does not acknowl- edge (ack = 1). a slave sends an acknowledge when it has recognized its address (including a general call), or when the slave has properly received its data. 16.4.11 i 2 c master mode reception master mode reception is enabled by programming the receive enable bit, rcen (sspcon2<3>). the baud rate generator begins counting, and on each rollover, the state of the scl pin changes (high to low/ low to high) and data is shifted into the sspsr. after the falling edge of the eighth clock, the receive enable flag is automatically cleared, the contents of the sspsr are loaded into the sspbuf, the bf flag bit is set, the sspif flag bit is set and the baud rate genera- tor is suspended from counting, holding scl low. the mssp is now in idle state, awaiting the next com- mand. when the buffer is read by the cpu, the bf flag bit is automatically cleared. the user can then send an acknowledge bit at the end of reception, by setting the acknowledge sequence enable bit, acken (sspcon2<4>). 16.4.11.1 bf status flag in receive operation, the bf bit is set when an address or data byte is loaded into sspbuf from sspsr. it is cleared when the sspbuf register is read. 16.4.11.2 sspov status flag in receive operation, the sspov bit is set when 8 bits are received into the sspsr and the bf flag bit is already set from a previous reception. 16.4.11.3 wcol status flag if the user writes the sspbuf when a receive is already in progress (i.e., sspsr is still shifting in a data byte), the wcol bit is set and the contents of the buffer are unchanged (the write doesn?t occur). note: in the mssp module, the rcen bit must be set after the ack sequence or the rcen bit will be disregarded.
pic18fxx39 ds30485a-page 156 preliminary ? 2002 microchip technology inc. figure 16-21: i 2 c master mode waveform (transmission, 7 or 10-bit address) sda scl sspif bf (sspstat<0>) sen a7 a6 a5 a4 a3 a2 a1 ack = 0 d7d6d5d4d3d2d1d0 ack transmitting data or second half r/w = 0 transmit address to slave 123456789 123456789 p cleared in software service routine sspbuf is written in software from ssp interrupt after start condition, sen cleared by hardware s sspbuf written with 7-bit address and r/w start transmit scl held low while cpu responds to sspif sen = 0 of 10-bit address write sspcon2<0> sen = 1 start condition begins from slave clear ackstat bit sspcon2<6> ackstat in sspcon2 = 1 cleared in software sspbuf written pen cleared in software r/w
? 2002 microchip technology inc. preliminary ds30485a-page 157 pic18fxx39 figure 16-22: i 2 c master mode waveform (reception, 7-bit address) p 9 8 7 6 5 d0 d1 d2 d3 d4 d5 d6 d7 s a7 a6 a5 a4 a3 a2 a1 sda scl 12 3 4 5 6 7 8 9 12 3 4 5 678 9 1234 bus master terminates transfer ack receiving data from slave receiving data from slave d0 d1 d2 d3 d4 d5 d6 d7 ack r/w = 1 transmit address to slave sspif bf ack is not sent write to sspcon2<0> (sen = 1) write to sspbuf occurs here ack from slave master configured as a receiver by programming sspcon2<3>, (rcen = 1) pen bit = 1 written here data shifted in on falling edge of clk cleared in software start xmit sen = 0 sspov sda = 0, scl = 1 while cpu (sspstat<0>) ack last bit is shifted into sspsr and contents are unloaded into sspbuf cleared in software cleared in software set sspif interrupt at end of receive set p bit (sspstat<4>) and sspif cleared in software ack from master set sspif at end set sspif interrupt at end of acknowledge sequence set sspif interrupt at end of acknow- ledge sequence of receive set acken, start acknowledge sequence sspov is set because sspbuf is still full sda = ackdt = 1 rcen cleared automatically rcen = 1 start next receive write to sspcon2<4> to start acknowledge sequence sda = ackdt (sspcon2<5>) = 0 rcen cleared automatically responds to sspif acken begin start condition cleared in software sda = ackdt = 0
pic18fxx39 ds30485a-page 158 preliminary ? 2002 microchip technology inc. 16.4.12 acknowledge sequence timing an acknowledge sequence is enabled by setting the acknowledge sequence enable bit, acken (sspcon2<4>). when this bit is set, the scl pin is pulled low and the contents of the acknowledge data bit are presented on the sda pin. if the user wishes to gen- erate an acknowledge, then the ackdt bit should be cleared. if not, the user should set the ackdt bit before starting an acknowledge sequence. the baud rate gen- erator then counts for one rollover period (t brg ) and the scl pin is de-asserted (pulled high). when the scl pin is sampled high (clock arbitration), the baud rate gener- ator counts for t brg . the scl pin is then pulled low. fol- lowing this, the acken bit is automatically cleared, the baud rate generator is turned off and the mssp module then goes into idle mode (figure 16-23). 16.4.12.1 wcol status flag if the user writes the sspbuf when an acknowledge sequence is in progress, then wcol is set and the con- tents of the buffer are unchanged (the write doesn?t occur). 16.4.13 stop condition timing a stop bit is asserted on the sda pin at the end of a receive/transmit by setting the stop sequence enable bit, pen (sspcon2<2>). at the end of a receive/ transmit the scl line is held low after the falling edge of the ninth clock. when the pen bit is set, the master will assert the sda line low. when the sda line is sam- pled low, the baud rate generator is reloaded and counts down to ?0?. when the baud rate generator times out, the scl pin will be brought high, and one t brg (baud rate generator rollover count) later, the sda pin will be de-asserted. when the sda pin is sampled high while scl is high, the p bit (sspstat<4>) is set. a t brg later, the pen bit is cleared and the sspif bit is set (figure 16-24). 16.4.13.1 wcol status flag if the user writes the sspbuf when a stop sequence is in progress, then the wcol bit is set and the con- tents of the buffer are unchanged (the write doesn?t occur). figure 16-23: acknowledge sequence waveform figure 16-24: stop condition receive or transmit mode note: t brg = one baud rate generator period. sda scl set sspif at the end acknowledge sequence starts here, write to sspcon2 acken automatically cleared cleared in t brg t brg of receive ack 8 acken = 1, ackdt = 0 d0 9 sspif software set sspif at the end of acknowledge sequence cleared in software scl sda sda asserted low before rising edge of clock write to sspcon2 set pen falling edge of scl = 1 for t brg , followed by sda = 1 for t brg 9th clock scl brought high after t brg note: t brg = one baud rate generator period. t brg t brg after sda sampled high. p bit (sspstat<4>) is set. t brg to setup stop condition . ack p t brg pen bit (sspcon2<2>) is cleared by hardware and the sspif bit is set
? 2002 microchip technology inc. preliminary ds30485a-page 159 pic18fxx39 16.4.14 sleep operation while in sleep mode, the i 2 c module can receive addresses or data, and when an address match or complete byte transfer occurs, wake the processor from sleep (if the mssp interrupt is enabled). 16.4.15 effect of a reset a reset disables the mssp module and terminates the current transfer. 16.4.16 multi-master mode in multi-master mode, the interrupt generation on the detection of the start and stop conditions allows the determination of when the bus is free. the stop (p) and start (s) bits are cleared from a reset, or when the mssp module is disabled. control of the i 2 c bus may be taken when the p bit (sspstat<4>) is set, or the bus is idle, with both the s and p bits clear. when the bus is busy, enabling the ssp interrupt will generate the interrupt when the stop condition occurs. in multi-master operation, the sda line must be moni- tored for arbitration, to see if the signal level is the expected output level. this check is performed in hardware, with the result placed in the bclif bit. the states where arbitration can be lost are: ? address transfer ? data transfer ? a start condition ? a repeated start condition ? an acknowledge condition 16.4.17 multi -master communication, bus collision, and bus arbitration multi-master mode support is achieved by bus arbitra- tion. when the master outputs address/data bits onto the sda pin, arbitration takes place when the master outputs a '1' on sda, by letting sda float high and another master asserts a '0'. when the scl pin floats high, data should be stable. if the expected data on sda is a '1' and the data sampled on the sda pin = '0', then a bus collision has taken place. the master will set the bus collision interrupt flag bclif and reset the i 2 c port to its idle state (figure 16-25). if a transmit was in progress when the bus collision occurred, the transmission is halted, the bf flag is cleared, the sda and scl lines are de-asserted, and the sspbuf can be written to. when the user services the bus collision interrupt service routine, and if the i 2 c bus is free, the user can resume communication by asserting a start condition. if a start, repeated start, stop, or acknowledge condition was in progress when the bus collision occurred, the condition is aborted, the sda and scl lines are de-asserted, and the respective control bits in the sspcon2 register are cleared. when the user ser- vices the bus collision interrupt service routine, and if the i 2 c bus is free, the user can resume communication by asserting a start condition. the master will continue to monitor the sda and scl pins. if a stop condition occurs, the sspif bit will be set. a write to the sspbuf will start the transmission of data at the first data bit, regardless of where the transmitter left off when the bus collision occurred. in multi-master mode, the interrupt generation on the detection of start and stop conditions allows the determination of when the bus is free. control of the i 2 c bus can be taken when the p bit is set in the sspstat register, or the bus is idle and the s and p bits are cleared. figure 16-25: bus collision timing for transmit and acknowledge sda scl bclif sda released sda line pulled low by another source sample sda. while scl is high, data doesn?t match what is driven bus collision has occurred. set bus collision interrupt (bclif) by the master. by master data changes while scl = 0
pic18fxx39 ds30485a-page 160 preliminary ? 2002 microchip technology inc. 16.4.17.1 bus collision during a start condition during a start condition, a bus collision occurs if: a) sda or scl are sampled low at the beginning of the start condition (figure 16-26). b) scl is sampled low before sda is asserted low (figure 16-27). during a start condition, both the sda and the scl pins are monitored. if the sda pin is already low, or the scl pin is already low, then all of the following occur: ? the start condition is aborted, ? the bclif flag is set, and ? the mssp module is reset to its idle state (figure 16-26). the start condition begins with the sda and scl pins de-asserted. when the sda pin is sampled high, the baud rate generator is loaded from sspadd<6:0> and counts down to ?0?. if the scl pin is sampled low while sda is high, a bus collision occurs, because it is assumed that another master is attempting to drive a data '1' during the start condition. if the sda pin is sampled low during this count, the brg is reset and the sda line is asserted early (figure 16-28). if, however, a '1' is sampled on the sda pin, the sda pin is asserted low at the end of the brg count. the baud rate generator is then reloaded and counts down to ?0?, and during this time, if the scl pins are sampled as '0', a bus collision does not occur. at the end of the brg count, the scl pin is asserted low. figure 16-26: bus collision during start condition (sda only) note: the reason that bus collision is not a factor during a start condition, is that no two bus masters can assert a start condition at the exact same time. therefore, one master will always assert sda before the other. this condition does not cause a bus collision, because the two masters must be allowed to arbitrate the first address follow- ing the start condition. if the address is the same, arbitration must be allowed to continue into the data portion, repeated start or stop conditions. sda scl sen sda sampled low before sda goes low before the sen bit is set. s bit and sspif set because ssp module reset into idle state. sen cleared automatically because of bus collision. s bit and sspif set because set sen, enable start condition if sda = 1, scl = 1 sda = 0, scl = 1. bclif s sspif sda = 0, scl = 1. sspif and bclif are cleared in software. sspif and bclif are cleared in software. set bclif, start condition. set bclif.
? 2002 microchip technology inc. preliminary ds30485a-page 161 pic18fxx39 figure 16-27: bus collision during start condition (scl = 0) figure 16-28: brg reset due to sda arbitration during start condition sda scl sen bus collision occurs. set bclif. scl = 0 before sda = 0, set sen, enable start sequence if sda = 1, scl = 1 t brg t brg sda = 0, scl = 1 bclif s sspif interrupt cleared in software bus collision occurs. set bclif. scl = 0 before brg time-out, '0' '0' '0' '0' sda scl sen set s set sen, enable start sequence if sda = 1, scl = 1 less than t brg t brg sda = 0, scl = 1 bclif s sspif s interrupts cleared in software set sspif sda = 0, scl = 1 sda pulled low by other master. reset brg and assert sda. scl pulled low after brg time-out set sspif '0'
pic18fxx39 ds30485a-page 162 preliminary ? 2002 microchip technology inc. 16.4.17.2 bus collision during a repeated start condition during a repeated start condition, a bus collision occurs if: a) a low level is sampled on sda when scl goes from low level to high level. b) scl goes low before sda is asserted low, indi- cating that another master is attempting to transmit a data ?1?. when the user de-asserts sda and the pin is allowed to float high, the brg is loaded with sspadd<6:0> and counts down to ?0?. the scl pin is then de-asserted, and when sampled high, the sda pin is sampled. if sda is low, a bus collision has occurred (i.e., another master is attempting to transmit a data ?0?, figure 16-29). if sda is sampled high, the brg is reloaded and begins counting. if sda goes from high to low before the brg times out, no bus collision occurs because no two masters can assert sda at exactly the same time. if scl goes from high to low before the brg times out and sda has not already been asserted, a bus collision occurs. in this case, another master is attempting to transmit a data ?1? during the repeated start condition, see figure 16-30. if, at the end of the brg time-out, both scl and sda are still high, the sda pin is driven low and the brg is reloaded and begins counting. at the end of the count, regardless of the status of the scl pin, the scl pin is driven low and the repeated start condition is complete. figure 16-29: bus collision during a repeated start condition (case 1) figure 16-30: bus collision during repeated start condition (case 2) sda scl rsen bclif s sspif sample sda when scl goes high. if sda = 0, set bclif and release sda and scl. cleared in software '0' '0' sda scl bclif rsen s sspif interrupt cleared in software scl goes low before sda, set bclif. release sda and scl. t brg t brg '0'
? 2002 microchip technology inc. preliminary ds30485a-page 163 pic18fxx39 16.4.17.3 bus collision during a stop condition bus collision occurs during a stop condition if: a) after the sda pin has been de-asserted and allowed to float high, sda is sampled low after the brg has timed out. b) after the scl pin is de-asserted, scl is sampled low before sda goes high. the stop condition begins with sda asserted low. when sda is sampled low, the scl pin is allowed to float. when the pin is sampled high (clock arbitration), the baud rate generator is loaded with sspadd<6:0> and counts down to ?0?. after the brg times out, sda is sampled. if sda is sampled low, a bus collision has occurred. this is due to another master attempting to drive a data '0' (figure 16-31). if the scl pin is sampled low before sda is allowed to float high, a bus collision occurs. this is another case of another master attempting to drive a data '0' (figure 16-32). figure 16-31: bus collision during a stop condition (case 1) figure 16-32: bus collision during a stop condition (case 2) sda scl bclif pen p sspif t brg t brg t brg sda asserted low sda sampled low after t brg , set bclif '0' '0' sda scl bclif pen p sspif t brg t brg t brg assert sda scl goes low before sda goes high, set bclif '0' '0'
pic18fxx39 ds30485a-page 164 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 165 pic18fxx39 17.0 addressable universal synchronous asynchronous receiver transmitter (usart) the universal synchronous asynchronous receiver transmitter (usart) module is one of the two serial i/o modules. (usart is also known as a serial com- munications interface or sci.) the usart can be con- figured as a full-duplex asynchronous system that can communicate with peripheral devices, such as crt ter- minals and personal computers, or it can be configured as a half-duplex synchronous system that can commu- nicate with peripheral devices, such as a/d or d/a integrated circuits, serial eeproms, etc. the usart can be configured in the following modes: ? asynchronous (full-duplex) ? synchronous - master (half-duplex) ? synchronous - slave (half-duplex) in order to configure pins rc6/tx/ck and rc7/rx/dt as the universal synchronous asynchronous receiver transmitter: ? bit spen (rcsta<7>) must be set (= 1), ? bit trisc<6> must be cleared (= 0), and ? bit trisc<7> must be set (= 1). register 17-1 shows the transmit status and control register (txsta) and register 17-2 shows the receive status and control register (rcsta).
pic18fxx39 ds30485a-page 166 preliminary ? 2002 microchip technology inc. register 17-1: txsta: transmit status and control register r/w-0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r-1 r/w-0 csrc tx9 txen sync ?brghtrmttx9d bit 7 bit 0 bit 7 csrc: clock source select bit asynchronous mode: don?t care synchronous mode: 1 = master mode (clock generated internally from brg) 0 = slave mode (clock from external source) bit 6 tx9 : 9-bit transmit enable bit 1 = selects 9-bit transmission 0 = selects 8-bit transmission bit 5 txen : transmit enable bit 1 = transmit enabled 0 = transmit disabled note: sren/cren overrides txen in sync mode. bit 4 sync : usart mode select bit 1 = synchronous mode 0 = asynchronous mode bit 3 unimplemented: read as '0' bit 2 brgh : high baud rate select bit asynchronous mode: 1 = high speed 0 = low speed synchronous mode: unused in this mode bit 1 trmt : transmit shift register status bit 1 = tsr empty 0 = tsr full bit 0 tx9d: 9th bit of transmit data can be address/data bit or a parity bit legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2002 microchip technology inc. preliminary ds30485a-page 167 pic18fxx39 register 17-2: rcsta: receive status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-0 r-0 r-x spen rx9 sren cren adden ferr oerr rx9d bit 7 bit 0 bit 7 spen: serial port enable bit 1 = serial port enabled (configures rx/dt and tx/ck pins as serial port pins) 0 = serial port disabled bit 6 rx9 : 9-bit receive enable bit 1 = selects 9-bit reception 0 = selects 8-bit reception bit 5 sren : single receive enable bit asynchronous mode : don?t care synchronous mode - master: 1 = enables single receive 0 = disables single receive this bit is cleared after reception is complete. synchronous mode - slave: don?t care bit 4 cren : continuous receive enable bit asynchronous mode: 1 = enables receiver 0 = disables receiver synchronous mode: 1 = enables continuous receive until enable bit cren is cleared (cren overrides sren) 0 = disables continuous receive bit 3 adden : address detect enable bit asynchronous mode 9-bit (rx9 = 1): 1 = enables address detection, enables interrupt and load of the receive buffer when rsr<8> is set 0 = disables address detection, all bytes are received, and ninth bit can be used as parity bit bit 2 ferr : framing error bit 1 = framing error (can be updated by reading rcreg register and receive next valid byte) 0 = no framing error bit 1 oerr : overrun error bit 1 = overrun error (can be cleared by clearing bit cren) 0 = no overrun error bit 0 rx9d: 9th bit of received data this can be address/data bit or a parity bit, and must be calculated by user firmware legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 168 preliminary ? 2002 microchip technology inc. 17.1 usart baud rate generator (brg) the brg supports both the asynchronous and syn- chronous modes of the usart. it is a dedicated 8-bit baud rate generator. the spbrg register controls the period of a free running 8-bit timer. in asynchronous mode, bit brgh (txsta<2>) also controls the baud rate. in synchronous mode, bit brgh is ignored. table 17-1 shows the formula for computation of the baud rate for different usart modes, which only apply in master mode (internal clock). given the desired baud rate and f osc , the nearest integer value for the spbrg register can be calculated using the formula in table 17-1. from this, the error in baud rate can be determined. example 17-1 shows the calculation of the baud rate error for the following conditions: ?f osc = 16 mhz ? desired baud rate = 9600 ? brgh = 0 ? sync = 0 it may be advantageous to use the high baud rate (brgh = 1) even for slower baud clocks. this is because the f osc /(16(x + 1)) equation can reduce the baud rate error in some cases. writing a new value to the spbrg register causes the brg timer to be reset (or cleared). this ensures the brg does not wait for a timer overflow before outputting the new baud rate. 17.1.1 sampling the data on the rc7/rx/dt pin is sampled three times by a majority detect circuit to determine if a high or a low level is present at the rx pin. example 17-1: calculating baud rate error table 17-1: baud rate formula table 17-2: registers associated with baud rate generator desired baud rate = f osc / (64 (x + 1)) solving for x: x = ( (f osc / desired baud rate) / 64 ) ? 1 x = ((16000000 / 9600) / 64) ? 1 x = [25.042] = 25 calculated baud rate = 16000000 / (64 (25 + 1)) = 9615 error = (calculated baud rate ? desired baud rate) desired baud rate = (9615 ? 9600) / 9600 = 0.16% sync brgh = 0 (low speed) brgh = 1 (high speed) 0 1 (asynchronous) baud rate = f osc /(64(x+1)) (synchronous) baud rate = f osc /(4(x+1)) baud rate = f osc /(16(x+1)) n/a legend: x = value in spbrg (0 to 255) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets txsta csrc tx9 txen sync ?brgh trmt tx9d 0000 -010 0000 -010 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented, read as '0'. shaded cells are not used by the brg.
? 2002 microchip technology inc. preliminary ds30485a-page 169 pic18fxx39 table 17-3: baud rates for synchronous mode baud rate (kbps) f osc = 40 mhz spbrg value (decimal) 33 mhz spbrg value (decimal) 25 mhz spbrg value (decimal) 20 mhz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 na - - na - - na - - na - - 1.2 na - - na - - na - - na - - 2.4 na - - na - - na - - na - - 9.6 na - - na - - na - - na - - 19.2 na - - na - - na - - na - - 76.8 76.92 +0.16 129 77.10 +0.39 106 77.16 +0.47 80 76.92 +0.16 64 96 96.15 +0.16 103 95.93 -0.07 85 96.15 +0.16 64 96.15 +0.16 51 300 303.03 +1.01 32 294.64 -1.79 27 297.62 -0.79 20 294.12 -1.96 16 500 500 0 19 485.30 -2.94 16 480.77 -3.85 12 500 0 9 high 10000 - 0 8250 - 0 6250 - 0 5000 - 0 low 39.06 - 255 32.23 - 255 24.41 - 255 19.53 - 255 baud rate (kbps) f osc = 16 mhz spbrg value (decimal) 10 mhz spbrg value (decimal) 7.15909 mhz spbrg value (decimal) 5.0688 mhz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 na - - na - - na - - na - - 1.2 na - - na - - na - - na - - 2.4 na - - na - - na - - na - - 9.6 na - - na - - 9.62 +0.23 185 9.60 0 131 19.2 19.23 +0.16 207 19.23 +0.16 129 19.24 +0.23 92 19.20 0 65 76.8 76.92 +0.16 51 75.76 -1.36 32 77.82 +1.32 22 74.54 -2.94 16 96 95.24 -0.79 41 96.15 +0.16 25 94.20 -1.88 18 97.48 +1.54 12 300 307.70 +2.56 12 312.50 +4.17 7 298.35 -0.57 5 316.80 +5.60 3 500 500 0 7 500 0 4 447.44 -10.51 3 422.40 -15.52 2 high 4000 - 0 2500 - 0 1789.80 - 0 1267.20 - 0 low 15.63 - 255 9.77 - 255 6.99 - 255 4.95 - 255 baud rate (kbps) f osc = 4 mhz spbrg value (decimal) 3.579545 mhz spbrg value (decimal) 1 mhz spbrg value (decimal) 32.768 khz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 na - - na - - na - - 0.30 +1.14 26 1.2 na - - na - - 1.20 +0.16 207 1.17 -2.48 6 2.4 na - - na - - 2.40 +0.16 103 2.73 +13.78 2 9.6 9.62 +0.16 103 9.62 +0.23 92 9.62 +0.16 25 8.20 -14.67 0 19.2 19.23 +0.16 51 19.04 -0.83 46 19.23 +0.16 12 na - - 76.8 76.92 +0.16 12 74.57 -2.90 11 83.33 +8.51 2 na - - 96 1000 +4.17 9 99.43 +3.57 8 83.33 -13.19 2 na - - 300 333.33 +11.11 2 298.30 -0.57 2 250 -16.67 0 na - - 500 500 0 1 447.44 -10.51 1 na - - na - - high 1000 - 0 894.89 - 0 250 - 0 8.20 - 0 low 3.91 - 255 3.50 - 255 0.98 - 255 0.03 - 255
pic18fxx39 ds30485a-page 170 preliminary ? 2002 microchip technology inc. table 17-4: baud rates for asynchronous mode (brgh = 0) baud rate (kbps) f osc = 40 mhz spbrg value (decimal) 33 mhz spbrg value (decimal) 25 mhz spbrg value (decimal) 20 mhz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 na - - na - - na - - na - - 1.2 na - - na - - na - - na - - 2.4 na - - 2.40 -0.07 214 2.40 -0.15 162 2.40 +0.16 129 9.6 9.62 +0.16 64 9.55 -0.54 53 9.53 -0.76 40 9.47 -1.36 32 19.2 18.94 -1.36 32 19.10 -0.54 26 19.53 +1.73 19 19.53 +1.73 15 76.8 78.13 +1.73 7 73.66 -4.09 6 78.13 +1.73 4 78.13 +1.73 3 96 89.29 -6.99 6 103.13 +7.42 4 97.66 +1.73 3 104.17 +8.51 2 300 312.50 +4.17 1 257.81 -14.06 1 na - - 312.50 +4.17 0 500 625 +25.00 0 na - - na - - na - - high 625 - 0 515.63 - 0 390.63 - 0 312.50 - 0 low 2.44 - 255 2.01 - 255 1.53 - 255 1.22 - 255 baud rate (kbps) f osc = 16 mhz spbrg value (decimal) 10 mhz spbrg value (decimal) 7.15909 mhz spbrg value (decimal) 5.0688 mhz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 na - - na - - na - - na - - 1.2 1.20 +0.16 207 1.20 +0.16 129 1.20 +0.23 92 1.20 0 65 2.4 2.40 +0.16 103 2.40 +0.16 64 2.38 -0.83 46 2.40 0 32 9.6 9.62 +0.16 25 9.77 +1.73 15 9.32 -2.90 11 9.90 +3.13 7 19.2 19.23 +0.16 12 19.53 +1.73 7 18.64 -2.90 5 19.80 +3.13 3 76.8 83.33 +8.51 2 78.13 +1.73 1 111.86 +45.65 0 79.20 +3.13 0 96 83.33 -13.19 2 78.13 -18.62 1 na - - na - - 300 250 -16.67 0 156.25 -47.92 0 na - - na - - 500 na - - na - - na - - na - - high 250 - 0 156.25 - 0 111.86 - 0 79.20 - 0 low 0.98 - 255 0.61 - 255 0.44 - 255 0.31 - 255 baud rate (kbps) f osc = 4 mhz spbrg value (decimal) 3.579545 mhz spbrg value (decimal) 1 mhz spbrg value (decimal) 32.768 khz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 0.30 -0.16 207 0.30 +0.23 185 0.30 +0.16 51 0.26 -14.67 1 1.2 1.20 +1.67 51 1.19 -0.83 46 1.20 +0.16 12 na - - 2.4 2.40 +1.67 25 2.43 +1.32 22 2.23 -6.99 6 na - - 9.6 8.93 -6.99 6 9.32 -2.90 5 7.81 -18.62 1 na - - 19.2 20.83 +8.51 2 18.64 -2.90 2 15.63 -18.62 0 na - - 76.8 62.50 -18.62 0 55.93 -27.17 0 na - - na - - 96 na - - na - - na - - na - - 300 na - - na - - na - - na - - 500 na - - na - - na - - na - - high 62.50 - 0 55.93 - 0 15.63 - 0 0.51 - 0 low 0.24 - 255 0.22 - 255 0.06 - 255 0.002 - 255
? 2002 microchip technology inc. preliminary ds30485a-page 171 pic18fxx39 table 17-5: baud rates for asynchronous mode (brgh = 1) baud rate (kbps) f osc = 40 mhz spbrg value (decimal) 33 mhz spbrg value (decimal) 25 mhz spbrg value (decimal) 20 mhz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3na- -na- -na- -na- - 1.2na- -na- -na- -na- - 2.4na- -na- -na- -na- - 9.6 na - - 9.60 -0.07 214 9.59 -0.15 162 9.62 +0.16 129 19.2 19.23 +0.16 129 19.28 +0.39 106 19.30 +0.47 80 19.23 +0.16 64 76.8 75.76 -1.36 32 76.39 -0.54 26 78.13 +1.73 19 78.13 +1.73 15 96 96.15 +0.16 25 98.21 +2.31 20 97.66 +1.73 15 96.15 +0.16 12 300 312.50 +4.17 7 294.64 -1.79 6 312.50 +4.17 4 312.50 +4.17 3 500 500 0 4 515.63 +3.13 3 520.83 +4.17 2 416.67 -16.67 2 high 2500 - 0 2062.50 - 0 1562.50 - 0 1250 - 0 low 9.77 - 255 8,06 - 255 6.10 - 255 4.88 - 255 baud rate (kbps) f osc = 16 mhz spbrg value (decimal) 10 mhz spbrg value (decimal) 7.15909 mhz spbrg value (decimal) 5.0688 mhz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3na- - na- - na- - na- - 1.2na- - na- - na- - na- - 2.4 na - - na - - 2.41 +0.23 185 2.40 0 131 9.6 9.62 +0.16 103 9.62 +0.16 64 9.52 -0.83 46 9.60 0 32 19.2 19.23 +0.16 51 18.94 -1.36 32 19.45 +1.32 22 18.64 -2.94 16 76.8 76.92 +0.16 12 78.13 +1.73 7 74.57 -2.90 5 79.20 +3.13 3 96 100 +4.17 9 89.29 -6.99 6 89.49 -6.78 4 105.60 +10.00 2 300 333.33 +11.11 2 312.50 +4.17 1 447.44 +49.15 0 316.80 +5.60 0 500 500 0 1 625 +25.00 0 447.44 -10.51 0 na - - high 1000 - 0 625 - 0 447.44 - 0 316.80 - 0 low 3.91 - 255 2.44 - 255 1.75 - 255 1.24 - 255 baud rate (kbps) f osc = 4 mhz spbrg value (decimal) 3.579545 mhz spbrg value (decimal) 1 mhz spbrg value (decimal) 32.768 khz spbrg value (decimal) kbaud % error kbaud % error kbaud % error kbaud % error 0.3 na - - na - - 0.30 +0.16 207 0.29 -2.48 6 1.2 1.20 +0.16 207 1.20 +0.23 185 1.20 +0.16 51 1.02 -14.67 1 2.4 2.40 +0.16 103 2.41 +0.23 92 2.40 +0.16 25 2.05 -14.67 0 9.6 9.62 +0.16 25 9.73 +1.32 22 8.93 -6.99 6 na - - 19.2 19.23 +0.16 12 18.64 -2.90 11 20.83 +8.51 2 na - - 76.8 na - - 74.57 -2.90 2 62.50 -18.62 0 na - - 96 na - - 111.86 +16.52 1 na - - na - - 300 na - - 223.72 -25.43 0 na - - na - - 500na- - na- - na- - na- - high 250 - 0 55.93 - 0 62.50 - 0 2.05 - 0 low 0.98 - 255 0.22 - 255 0.24 - 255 0.008 - 255
pic18fxx39 ds30485a-page 172 preliminary ? 2002 microchip technology inc. 17.2 usart asynchronous mode in this mode, the usart uses standard non-return-to- zero (nrz) format (one start bit, eight or nine data bits and one stop bit). the most common data format is 8-bits. an on-chip dedicated 8-bit baud rate genera- tor can be used to derive standard baud rate frequen- cies from the oscillator. the usart transmits and receives the lsb first. the usart?s transmitter and receiver are functionally independent, but use the same data format and baud rate. the baud rate gener- ator produces a clock, either x16 or x64 of the bit shift rate, depending on bit brgh (txsta<2>). parity is not supported by the hardware, but can be implemented in software (and stored as the ninth data bit). asynchronous mode is stopped during sleep. asynchronous mode is selected by clearing bit sync (txsta<4>). the usart asynchronous module consists of the following important elements: ? baud rate generator ? sampling circuit ? asynchronous transmitter ? asynchronous receiver 17.2.1 usart asynchronous transmitter the usart transmitter block diagram is shown in figure 17-1. the heart of the transmitter is the transmit (serial) shift register (tsr). the shift register obtains its data from the read/write transmit buffer, txreg. the txreg register is loaded with data in software. the tsr register is not loaded until the stop bit has been transmitted from the previous load. as soon as the stop bit is transmitted, the tsr is loaded with new data from the txreg register (if available). once the txreg register transfers the data to the tsr register (occurs in one t cy ), the txreg register is empty and flag bit txif (pir1<4>) is set. this interrupt can be enabled/disabled by setting/clearing enable bit txie ( pie1<4>). flag bit txif will be set, regardless of the state of enable bit txie and cannot be cleared in soft- ware. it will reset only when new data is loaded into the txreg register. while flag bit txif indicated the sta- tus of the txreg register, another bit, trmt (txsta<1>), shows the status of the tsr register. sta- tus bit trmt is a read only bit, which is set when the tsr register is empty. no interrupt logic is tied to this bit, so the user has to poll this bit in order to determine if the tsr register is empty. to set up an asynchronous transmission: 1. initialize the spbrg register for the appropriate baud rate. if a high speed baud rate is desired, set bit brgh (section 17.1). 2. enable the asynchronous serial port by clearing bit sync and setting bit spen. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set transmit bit tx9. can be used as address/data bit. 5. enable the transmission by setting bit txen, which will also set bit txif. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. load data to the txreg register (starts transmission). figure 17-1: usart transmit block diagram note 1: the tsr register is not mapped in data memory, so it is not available to the user. 2: flag bit txif is set when enable bit txen is set. note: txif is not cleared immediately upon load- ing data into the transmit buffer txreg. the flag bit becomes valid in the second instruction cycle following the load instruction. txif txie interrupt txen baud rate clk spbrg baud rate generator tx9d msb lsb data bus txreg register tsr register (8) 0 tx9 trmt spen rc6/tx/ck pin pin buffer and control 8 ? ? ?
? 2002 microchip technology inc. preliminary ds30485a-page 173 pic18fxx39 figure 17-2: asynchronous transmission figure 17-3: asynchronous transmission (back to back) table 17-6: registers associated with asynchronous transmission word 1 stop bit word 1 transmit shift reg start bit bit 0 bit 1 bit 7/8 write to txreg word 1 brg output (shift clock) rc6/tx/ck (pin) txif bit (transmit buffer reg. empty flag) trmt bit (transmit shift reg. empty flag) transmit shift reg. write to txreg brg output (shift clock) rc6/tx/ck (pin) txif bit (interrupt reg. flag) trmt bit (transmit shift reg. empty flag) word 1 word 2 word 1 word 2 start bit stop bit start bit transmit shift reg. word 1 word 2 bit 0 bit 1 bit 7/8 bit 0 note: this timing diagram shows two consecutive transmissions. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x txreg usart transmit register 0000 0000 0000 0000 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 0000 -010 spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented locations read as '0'. shaded cells are not used for asynchronous transmission. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
pic18fxx39 ds30485a-page 174 preliminary ? 2002 microchip technology inc. 17.2.2 usart asynchronous receiver the receiver block diagram is shown in figure 17-4. the data is received on the rc7/rx/dt pin and drives the data recovery block. the data recovery block is actually a high speed shifter operating at x16 times the baud rate, whereas the main receive serial shifter oper- ates at the bit rate or at f osc . this mode would typically be used in rs-232 systems. to set up an asynchronous reception: 1. initialize the spbrg register for the appropriate baud rate. if a high speed baud rate is desired, set bit brgh (section 17.1). 2. enable the asynchronous serial port by clearing bit sync and setting bit spen. 3. if interrupts are desired, set enable bit rcie. 4. if 9-bit reception is desired, set bit rx9. 5. enable the reception by setting bit cren. 6. flag bit rcif will be set when reception is com- plete and an interrupt will be generated if enable bit rcie was set. 7. read the rcsta register to get the ninth bit (if enabled) and determine if any error occurred during reception. 8. read the 8-bit received data by reading the rcreg register. 9. if any error occurred, clear the error by clearing enable bit cren. 10. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. 17.2.3 setting up 9-bit mode with address detect this mode would typically be used in rs-485 systems. to set up an asynchronous reception with address detect enable: 1. initialize the spbrg register for the appropriate baud rate. if a high speed baud rate is required, set the brgh bit. 2. enable the asynchronous serial port by clearing the sync bit and setting the spen bit. 3. if interrupts are required, set the rcen bit and select the desired priority level with the rcip bit. 4. set the rx9 bit to enable 9-bit reception. 5. set the adden bit to enable address detect. 6. enable reception by setting the cren bit. 7. the rcif bit will be set when reception is com- plete. the interrupt will be acknowledged if the rcie and gie bits are set. 8. read the rcsta register to determine if any error occurred during reception, as well as read bit 9 of data (if applicable). 9. read rcreg to determine if the device is being addressed. 10. if any error occurred, clear the cren bit. 11. if the device has been addressed, clear the adden bit to allow all received data into the receive buffer and interrupt the cpu. figure 17-4: usart receive block diagram x64 baud rate clk spbrg baud rate generator rc7/rx/dt pin buffer and control spen data recovery cren oerr ferr rsr register msb lsb rx9d rcreg register fifo interrupt rcif rcie data bus 8 64 16 or stop start (8) 7 1 0 rx9 ? ? ?
? 2002 microchip technology inc. preliminary ds30485a-page 175 pic18fxx39 figure 17-5: asynchronous reception table 17-7: registers associated with asynchronous reception start bit bit7/8 bit1 bit0 bit7/8 bit0 stop bit start bit start bit bit7/8 stop bit rx (pin) reg rcv buffer reg rcv shift read rcv buffer reg rcreg rcif (interrupt flag) oerr bit cren word 1 rcreg word 2 rcreg stop bit note: this timing diagram shows three words appearing on the rx input. the rcreg (receive buffer) is read after the third word, cau sing the oerr (overrun) bit to be set. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x rcreg usart receive register 0000 0000 0000 0000 txsta csrc tx9 txen sync ?brgh trmt tx9d 0000 -010 0000 -010 spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented locations read as '0'. shaded cells are not used for asynchronous reception. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
pic18fxx39 ds30485a-page 176 preliminary ? 2002 microchip technology inc. 17.3 usart synchronous master mode in synchronous master mode, the data is transmitted in a half-duplex manner (i.e., transmission and reception do not occur at the same time). when transmitting data, the reception is inhibited and vice versa. synchronous mode is entered by setting bit sync (txsta<4>). in addition, enable bit spen (rcsta<7>) is set in order to configure the rc6/tx/ck and rc7/rx/dt i/o pins to ck (clock) and dt (data) lines, respectively. the master mode indicates that the processor transmits the master clock on the ck line. the master mode is entered by setting bit csrc (txsta<7>). 17.3.1 usart synchronous master transmission the usart transmitter block diagram is shown in figure 17-1. the heart of the transmitter is the transmit (serial) shift register (tsr). the shift register obtains its data from the read/write transmit buffer register txreg. the txreg register is loaded with data in software. the tsr register is not loaded until the last bit has been transmitted from the previous load. as soon as the last bit is transmitted, the tsr is loaded with new data from the txreg (if available). once the txreg register transfers the data to the tsr register (occurs in one t cycle ), the txreg is empty and inter- rupt bit txif (pir1<4>) is set. the interrupt can be enabled/disabled by setting/clearing enable bit txie (pie1<4>). flag bit txif will be set, regardless of the state of enable bit txie, and cannot be cleared in soft- ware. it will reset only when new data is loaded into the txreg register. while flag bit txif indicates the status of the txreg register, another bit trmt (txsta<1>) shows the status of the tsr register. trmt is a read only bit, which is set when the tsr is empty. no inter- rupt logic is tied to this bit, so the user has to poll this bit in order to determine if the tsr register is empty. the tsr is not mapped in data memory, so it is not available to the user. to set up a synchronous master transmission: 1. initialize the spbrg register for the appropriate baud rate (section 17.1). 2. enable the synchronous master serial port by setting bits sync, spen, and csrc. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set bit tx9. 5. enable the transmission by setting bit txen. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. start transmission by loading data to the txreg register. table 17-8: registers associated with synchronous master transmission note: txif is not cleared immediately upon load- ing data into the transmit buffer txreg. the flag bit becomes valid in the second instruction cycle following the load instruction. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x txreg usart transmit register 0000 0000 0000 0000 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 0000 -010 spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented, read as '0'. shaded cells are not used for synchronous master transmission. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
? 2002 microchip technology inc. preliminary ds30485a-page 177 pic18fxx39 figure 17-6: synchronous transmission figure 17-7: synchronous transmission (through txen) bit 0 bit 1 bit 7 word 1 q1 q2 q3q4 q1 q2 q3 q4 q1 q2q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q3q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 bit 2 bit 0 bit 1 bit 7 rc7/rx/dt rc6/tx/ck write to txreg reg txif bit (interrupt flag) trmt txen bit '1' '1' word 2 trmt bit write word1 write word2 note: sync master mode; spbrg = '0'. continuous transmission of two 8-bit words. pin pin rc7/rx/dt pin rc6/tx/ck pin write to txreg reg txif bit trmt bit bit0 bit1 bit2 bit6 bit7 txen bit
pic18fxx39 ds30485a-page 178 preliminary ? 2002 microchip technology inc. 17.3.2 usart synchronous master reception once synchronous mode is selected, reception is enabled by setting either enable bit sren (rcsta<5>), or enable bit cren (rcsta<4>). data is sampled on the rc7/rx/dt pin on the falling edge of the clock. if enable bit sren is set, only a single word is received. if enable bit cren is set, the reception is continuous until cren is cleared. if both bits are set, then cren takes precedence. to set up a synchronous master reception: 1. initialize the spbrg register for the appropriate baud rate (section 17.1). 2. enable the synchronous master serial port by setting bits sync, spen and csrc. 3. ensure bits cren and sren are clear. 4. if interrupts are desired, set enable bit rcie. 5. if 9-bit reception is desired, set bit rx9. 6. if a single reception is required, set bit sren. for continuous reception, set bit cren. 7. interrupt flag bit rcif will be set when reception is complete and an interrupt will be generated if the enable bit rcie was set. 8. read the rcsta register to get the ninth bit (if enabled) and determine if any error occurred during reception. 9. read the 8-bit received data by reading the rcreg register. 10. if any error occurred, clear the error by clearing bit cren. 11. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. table 17-9: registers associated with synchronous master reception figure 17-8: synchronous reception (master mode, sren) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x rcreg usart receive register 0000 0000 0000 0000 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 0000 -010 spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented, read as '0'. shaded cells are not used for synchronous master reception. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear. cren bit rc7/rx/dt pin rc6/tx/ck pin write to bit sren sren bit rcif bit (interrupt) read rxreg q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q2 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4q1 q2 q3 q4 q1 q2 q3 q4 '0' bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 '0' q1 q2 q3 q4 note: timing diagram demonstrates sync master mode with bit sren = '1' and bit brgh = '0'.
? 2002 microchip technology inc. preliminary ds30485a-page 179 pic18fxx39 17.4 usart synchronous slave mode synchronous slave mode differs from the master mode in the fact that the shift clock is supplied externally at the rc6/tx/ck pin (instead of being supplied internally in master mode). this allows the device to transfer or receive data while in sleep mode. slave mode is entered by clearing bit csrc (txsta<7>). 17.4.1 usart synchronous slave transmit the operation of the synchronous master and slave modes are identical, except in the case of the sleep mode. if two words are written to the txreg and then the sleep instruction is executed, the following will occur: a) the first word will immediately transfer to the tsr register and transmit. b) the second word will remain in txreg register. c) flag bit txif will not be set. d) when the first word has been shifted out of tsr, the txreg register will transfer the second word to the tsr and flag bit txif will now be set. e) if enable bit txie is set, the interrupt will wake the chip from sleep. if the global interrupt is enabled, the program will branch to the interrupt vector. to set up a synchronous slave transmission: 1. enable the synchronous slave serial port by set- ting bits sync and spen and clearing bit csrc. 2. clear bits cren and sren. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set bit tx9. 5. enable the transmission by setting enable bit txen. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. start transmission by loading data to the txreg register. 8. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. table 17-10: registers associated with synchronous slave transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x txreg usart transmit register 0000 0000 0000 0000 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 0000 -010 spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented, read as '0'. shaded cells are not used for synchronous slave transmission. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
pic18fxx39 ds30485a-page 180 preliminary ? 2002 microchip technology inc. 17.4.2 usart synchronous slave reception the operation of the synchronous master and slave modes is identical, except in the case of the sleep mode and bit sren, which is a ?don't care? in slave mode. if receive is enabled by setting bit cren prior to the sleep instruction, then a word may be received during sleep. on completely receiving the word, the rsr register will transfer the data to the rcreg register, and if enable bit rcie bit is set, the interrupt generated will wake the chip from sleep. if the global interrupt is enabled, the program will branch to the interrupt vector. to set up a synchronous slave reception: 1. enable the synchronous master serial port by setting bits sync and spen and clearing bit csrc. 2. if interrupts are desired, set enable bit rcie. 3. if 9-bit reception is desired, set bit rx9. 4. to enable reception, set enable bit cren. 5. flag bit rcif will be set when reception is com- plete. an interrupt will be generated if enable bit rcie was set. 6. read the rcsta register to get the ninth bit (if enabled) and determine if any error occurred during reception. 7. read the 8-bit received data by reading the rcreg register. 8. if any error occurred, clear the error by clearing bit cren. 9. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. table 17-11: registers associated with synchronous slave reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x rcreg usart receive register 0000 0000 0000 0000 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 0000 -010 spbrg baud rate generator register 0000 0000 0000 0000 legend: x = unknown, - = unimplemented, read as '0'. shaded cells are not used for synchronous slave reception. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
? 2002 microchip technology inc. preliminary ds30485a-page 181 pic18fxx39 18.0 compatible 10-bit analog-to-digital converter (a/d) module the analog-to-digital (a/d) converter module has five inputs for the pic18f2x39 devices and eight for the pic18f4x39 devices. this module has the adcon0 and adcon1 register definitions that are compatible with the mid-range a/d module. the a/d allows conversion of an analog input signal to a corresponding 10-bit digital number. the a/d module has four registers: ? a/d result high register (adresh) ? a/d result low register (adresl) ? a/d control register 0 (adcon0) ? a/d control register 1 (adcon1) the adcon0 register, shown in register 18-1, con- trols the operation of the a/d module. the adcon1 register, shown in register 18-2, configures the functions of the port pins. register 18-1: adcon0 register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 adcs1 adcs0 chs2 chs1 chs0 go/done ?adon bit 7 bit 0 bit 7-6 adcs1:adcs0: a/d conversion clock select bits (adcon0 bits in bold ) bit 5-3 chs2:chs0: analog channel select bits 000 = channel 0 (an0) 001 = channel 1 (an1) 010 = channel 2 (an2) 011 = channel 3 (an3) 100 = channel 4 (an4) 101 = channel 5 (an5) (1) 110 = channel 6 (an6) (1) 111 = channel 7 (an7) (1) note 1: these channels are unimplemented on pic18f2x39 devices. do not select any unimplemented channel. bit 2 go/done : a/d conversion status bit when adon = 1: 1 = a/d conversion in progress (setting this bit starts the a/d conversion, which is automatically cleared by hardware when the a/d conversion is complete) 0 = a/d conversion not in progress bit 1 unimplemented: read as '0' bit 0 adon: a/d on bit 1 = a/d converter module is powered up 0 = a/d converter module is shut-off and consumes no operating current legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown adcon1 adcon0 clock conversion 0 00 f osc /2 0 01 f osc /8 0 10 f osc /32 0 11 f rc (clock derived from the internal a/d rc oscillator) 1 00 f osc /4 1 01 f osc /16 1 10 f osc /64 1 11 f rc (clock derived from the internal a/d rc oscillator)
pic18fxx39 ds30485a-page 182 preliminary ? 2002 microchip technology inc. register 18-2: adcon1 register r/w-0 r/w-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 adfm adcs2 ? ? pcfg3 pcfg2 pcfg1 pcfg0 bit 7 bit 0 bit 7 adfm: a/d result format select bit 1 = right justified. six (6) most significant bits of adresh are read as ?0?. 0 = left justified. six (6) least significant bits of adresl are read as ?0?. bit 6 adcs2: a/d conversion clock select bit (adcon1 bits in bold ) bit 5-4 unimplemented: read as '0' bit 3-0 pcfg3:pcfg0: a/d port configuration control bits legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: on any device reset, the port pins that are multiplexed with analog functions (anx) are forced to be an analog input. adcon1 adcon0 clock conversion 0 00 f osc /2 0 01 f osc /8 0 10 f osc /32 0 11 f rc (clock derived from the internal a/d rc oscillator) 1 00 f osc /4 1 01 f osc /16 1 10 f osc /64 1 11 f rc (clock derived from the internal a/d rc oscillator) a = analog input d = digital i/o c/r = # of analog input channels / # of a/d voltage references pcfg <3:0> an7 an6 an5 an4 an3 an2 an1 an0 v ref +v ref -c / r 0000 aaaa a a aav dd v ss 8 / 0 0001 aaaav ref +a aaan3v ss 7 / 1 0010 ddda a a aav dd v ss 5 / 0 0011 dddav ref +a aaan3v ss 4 / 1 0100 dddd a d aav dd v ss 3 / 0 0101 ddddv ref +d a aan3v ss 2 / 1 011x dddd d d dd ? ?0 / 0 1000 aaaav ref +v ref -a a an3an26 / 2 1001 ddaa a a aav dd v ss 6 / 0 1010 ddaav ref +a aaan3v ss 5 / 1 1011 ddaav ref +v ref -a a an3an24 / 2 1100 dddav ref +v ref -a a an3an23 / 2 1101 ddddv ref +v ref -a a an3an22 / 2 1110 dddd d d dav dd v ss 1 / 0 1111 ddddv ref +v ref -d a an3an21 / 2
? 2002 microchip technology inc. preliminary ds30485a-page 183 pic18fxx39 the analog reference voltage is software selectable to either the device?s positive and negative supply voltage (v dd and v ss ), or the voltage level on the ra3/an3/ v ref + pin and ra2/an2/v ref - pin. the a/d converter has a unique feature of being able to operate while the device is in sleep mode. to oper- ate in sleep, the a/d conversion clock must be derived from the a/d?s internal rc oscillator. the output of the sample and hold is the input into the converter, which generates the result via successive approximation. a device reset forces all registers to their reset state. this forces the a/d module to be turned off and any conversion is aborted. each port pin associated with the a/d converter can be configured as an analog input (ra3 can also be a voltage reference) or as a digital i/o. the adresh and adresl registers contain the result of the a/d conversion. when the a/d conversion is complete, the result is loaded into the adresh/ adresl registers, the go/done bit (adcon0<2>) is cleared, and a/d interrupt flag bit, adif is set. the block diagram of the a/d module is shown in figure 18-1. figure 18-1: a/d block diagram (input voltage) v ain v ref + reference voltage v dd pcfg<3:0> chs<2:0> an7* an6* an5* an4 an3 an2 an1 an0 111 110 101 100 011 010 001 000 10-bit converter v ref - v ss a/d * these channels are implemented only on the pic18f4x39 devices.
pic18fxx39 ds30485a-page 184 preliminary ? 2002 microchip technology inc. the value that is in the adresh/adresl registers is not modified for a power-on reset. the adresh/ adresl registers will contain unknown data after a power-on reset. after the a/d module has been configured as desired, the selected channel must be acquired before the con- version is started. the analog input channels must have their corresponding tris bits selected as an input. to determine acquisition time, see section 18.1. after this acquisition time has elapsed, the a/d conver- sion can be started. the following steps should be followed for doing an a/d conversion: 1. configure the a/d module: ? configure analog pins, voltage reference and digital i/o (adcon1) ? select a/d input channel (adcon0) ? select a/d conversion clock (adcon0) ? turn on a/d module (adcon0) 2. configure a/d interrupt (if desired): ? clear adif bit ? set adie bit ? set gie bit ? set peie bit 3. wait the required acquisition time. 4. start conversion: ? set go/done bit (adcon0) 5. wait for a/d conversion to complete, by either: ? polling for the go/done bit to be cleared (interrupts disabled) or ? waiting for the a/d interrupt 6. read a/d result registers (adresh/adresl); clear bit adif if required. 7. for next conversion, go to step 1 or step 2 as required. the a/d conversion time per bit is defined as t ad . a minimum wait of 2 t ad is required before the next acquisition starts. 18.1 a/d acquisition requirements for the a/d converter to meet its specified accuracy, the charge holding capacitor (c hold ) must be allowed to fully charge to the input channel voltage level. the analog input model is shown in figure 18-2. the source impedance (r s ) and the internal sampling switch (r ss ) impedance directly affect the time required to charge the capacitor c hold . the sampling switch (r ss ) impedance varies over the device voltage (v dd ). the source impedance affects the offset voltage at the analog input (due to pin leakage current). the maximum recommended impedance for analog sources is 2.5 k ? . after the analog input channel is selected (changed), this acquisition must be done before the conversion can be started. figure 18-2: analog input model note: when the conversion is started, the hold- ing capacitor is disconnected from the input pin. v ain c pin rs anx 5 pf v dd v t = 0.6v v t = 0.6v i leakage r ic 1k sampling switch ss r ss c hold = 120 pf v ss 6v sampling switch 5v 4v 3v 2v 567891011 (k ? ) v dd 500 na legend: c pin v t i leakage r ic ss c hold = input capacitance = threshold voltage = leakage current at the pin due to = interconnect resistance = sampling switch = sample/hold capacitance (from dac) various junctions
? 2002 microchip technology inc. preliminary ds30485a-page 185 pic18fxx39 to calculate the minimum acquisition time, equation 18-1 may be used. this equation assumes that 1/2 lsb error is used (1024 steps for the a/d). the 1/2 lsb error is the maximum error allowed for the a/d to meet its specified resolution. equation 18-1: a cquisition time equation 18-2: a/d minimum charging time example 18-1 shows the calculation of the minimum required acquisition time, t acq . this calculation is based on the following application system assumptions: ?c hold = 120 pf ?rs = 2.5 k ? ? conversion error 1/2 lsb ?v dd = 5v rss = 7 k ? ? temperature = 50 c (system max.) ?v hold = 0v @ time = 0 example 18-1: calculating the minimum required acquisition time t acq = amplifier settling time + holding capacitor charging time + temperature coefficient =t amp + t c + t coff v hold = (v ref ? (v ref /2048)) ? (1 ? e (-tc/c hold (r ic + r ss + r s )) ) or t c = -(120 pf)(1 k ? + r ss + r s ) ln(1/2048) t acq =t amp + t c + t coff temperature coefficient is only required for temperatures > 25 c. t acq =2 s + t c + [(temp ? 25 c)(0.05 s/ c)] t c =-c hold (r ic + r ss + r s ) ln(1/2048) -120 pf (1 k ? + 7 k ? + 2.5 k ? ) ln(0.0004883) -120 pf (10.5 k ? ) ln(0.0004883) -1.26 s (-7.6246) 9.61 s t acq =2 s + 9.61 s + [(50 c ? 25 c)(0.05 s/ c)] 11.61 s + 1.25 s 12.86 s
pic18fxx39 ds30485a-page 186 preliminary ? 2002 microchip technology inc. 18.2 selecting the a/d conversion clock the a/d conversion time per bit is defined as t ad . the a/d conversion requires 12 t ad per 10-bit conversion. the source of the a/d conversion clock is software selectable. the seven possible options for t ad are: ?2 t osc ?4 t osc ?8 t osc ?16 t osc ?32 t osc ?64 t osc ? internal a/d module rc oscillator (2-6 s) for correct a/d conversions, the a/d conversion clock (t ad ) must be selected to ensure a minimum t ad time of 1.6 s. table 18-1 shows the resultant t ad times derived from the device operating frequencies and the a/d clock source selected. 18.3 configuring analog port pins the adcon1, trisa and trise registers control the operation of the a/d port pins. the port pins, that are desired as analog inputs, must have their corresponding tris bits set (input). if the tris bit is cleared (output), the digital output level (v oh or v ol ) will be converted. the a/d operation is independent of the state of the chs2:chs0 bits and the tris bits. table 18-1: t ad vs. device operating frequencies note 1: when reading the port register, all pins configured as analog input channels will read as cleared (a low level). pins config- ured as digital inputs will convert an analog input. analog levels on a digitally config- ured input will not affect the conversion accuracy. 2: analog levels on any pin that is defined as a digital input (including the an4:an0 pins) may cause the input buffer to con- sume current that is out of the device?s specification. ad clock source (t ad ) maximum device frequency operation adcs2:adcs0 pic18fxx39 pic18lfxx39 2 t osc 000 1.25 mhz 666 khz 4 t osc 100 2.50 mhz 1.33 mhz 8 t osc 001 5.00 mhz 2.67 mhz 16 t osc 101 10.00 mhz 5.33 mhz 32 t osc 010 20.00 mhz 10.67 mhz 64 t osc 110 40.00 mhz 21.33 mhz rc 011 ??
? 2002 microchip technology inc. preliminary ds30485a-page 187 pic18fxx39 18.4 a/d conversions figure 18-3 shows the operation of the a/d converter after the go bit has been set. clearing the go/done bit during a conversion will abort the current conver- sion. the a/d result register pair will not be updated with the partially completed a/d conversion sample. that is, the adresh:adresl registers will continue to contain the value of the last completed conversion (or the last value written to the adresh:adresl reg- isters). after the a/d conversion is aborted, a 2 t ad wait is required before the next acquisition is started. after this 2 t ad wait, acquisition on the selected channel is automatically started. the go/done bit can then be set to start the conversion. figure 18-3: a/d conversion t ad cycles 18.4.1 a/d result registers the adresh:adresl register pair is the location where the 10-bit a/d result is loaded at the completion of the a/d conversion. this register pair is 16-bits wide. the a/d module gives the flexibility to left or right justify the 10-bit result in the 16-bit result register. the a/d format select bit (adfm) controls this justification. figure 18-4 shows the operation of the a/d result justi- fication. the extra bits are loaded with ?0?s. when an a/d result will not overwrite these locations (a/d disable), these registers may be used as two general purpose 8-bit registers. figure 18-4: a/d result justification note: the go/done bit should not be set in the same instruction that turns on the a/d. t ad 1 t ad 2 t ad 3 t ad 4 t ad 5 t ad 6 t ad 7 t ad 8 t ad 11 set go bit holding capacitor is disconnected from analog input (typically 100 ns) b9 b8 b7 b6 b5 b4 b3 b2 t ad 9 t ad 10 b1 b0 t cy - t ad next q4: adresh/adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. conversion starts b0 10-bit result adresh adresl 0000 00 adfm = 0 0 2 1 0 7 7 10-bit result adresh adresl 10-bit result 0000 00 7 0 7 6 5 0 adfm = 1 right justified left justified
pic18fxx39 ds30485a-page 188 preliminary ? 2002 microchip technology inc. table 18-2: summary of a/d registers name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ? tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ? tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip (1) adip rcip txip sspip ? tmr2ip tmr1ip 0000 0000 0000 0000 pir2 ? ? ? eeif bclif lvdif tmr3if ? ---0 0000 ---0 0000 pie2 ? ? ? eeie bclie lvdie tmr3ie ? ---0 0000 ---0 0000 ipr2 ? ? ? eeip bclip lvdip tmr3ip ? ---1 1111 ---1 0000 adresh a/d result register xxxx xxxx uuuu uuuu adresl a/d result register xxxx xxxx uuuu uuuu adcon0 adcs1 adcs0 chs2 chs1 chs0 go/done ?adon 0000 00-0 0000 00-0 adcon1 adfm adcs2 ? ? pcfg3 pcfg2 pcfg1 pcfg0 ---- -000 ---- -000 porta ? ra6 ra5 ra4 ra3 ra2 ra1 ra0 --0x 0000 --0u 0000 trisa ? porta data direction register --11 1111 --11 1111 porte ? ? ? ? ?re2re1re0 ---- -000 ---- -000 late ? ? ? ? ? late2 late1 late0 ---- -xxx ---- -uuu trise ibf obf ibov pspmode ? porte data direction bits 0000 -111 0000 -111 legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. shaded cells are not used for a/d conversion. note 1: the pspif, pspie and pspip bits are reserved on the pic18f2x39 devices; always maintain these bits clear.
? 2002 microchip technology inc. preliminary ds30485a-page 189 pic18fxx39 19.0 low voltage detect in many applications, the ability to determine if the device voltage (v dd ) is below a specified voltage level is a desirable feature. a window of operation for the application can be created, where the application soft- ware can do ?housekeeping tasks? before the device voltage exits the valid operating range. this can be done using the low voltage detect module. this module is a software programmable circuitry, where a device voltage trip point can be specified. when the voltage of the device becomes lower then the specified point, an interrupt flag is set. if the interrupt is enabled, the program execution will branch to the inter- rupt vector address and the software can then respond to that interrupt source. the low voltage detect circuitry is completely under software control. this allows the circuitry to be ?turned off? by the software, which minimizes the current consumption for the device. figure 19-1 shows a possible application voltage curve (typically for batteries). over time, the device voltage decreases. when the device voltage equals voltage v a , the lvd logic generates an interrupt. this occurs at time t a . the application software then has the time, until the device voltage is no longer in valid operating range, to shutdown the system. voltage point v b is the minimum valid operating voltage specification. this occurs at time t b . the difference t b ? t a is the total time for shutdown. figure 19-1: typical low voltage detect application the block diagram for the lvd module is shown in figure 19-2. a comparator uses an internally gener- ated reference voltage as the set point. when the selected tap output of the device voltage crosses the set point (is lower than), the lvdif bit is set. each node in the resistor divider represents a ?trip point? voltage. the ?trip point? voltage is the minimum supply voltage level at which the device can operate before the lvd module asserts an interrupt. when the supply voltage is equal to the trip point, the voltage tapped off of the resistor array is equal to the 1.2v internal reference voltage generated by the voltage reference module. the comparator then generates an interrupt signal setting the lvdif bit. this voltage is software programmable to any one of 16 values (see figure 19-2). the trip point is selected by programming the lvdl3:lvdl0 bits (lvdcon<3:0>). time voltage v a v b t a t b v a = lvd trip point v b = minimum valid device operating voltage legend:
pic18fxx39 ds30485a-page 190 preliminary ? 2002 microchip technology inc. figure 19-2: low voltage detect (lvd) block diagram the lvd module has an additional feature that allows the user to supply the trip voltage to the module from an external source. this mode is enabled when bits lvdl3:lvdl0 are set to ? 1111 ?. in this state, the com- parator input is multiplexed from the external input pin, lvdin (figure 19-3). this gives users flexibility, because it allows them to configure the low voltage detect interrupt to occur at any voltage in the valid operating range. figure 19-3: low voltage detect (lvd) with external input block diagram lvdif v dd 16 to 1 mux lvden lvd control register internally generated reference voltage lvdin 1.2v typical ? + lvd en lvd control 16 to 1 mux bgap boden lvden vxen lvdin register v dd v dd externally generated trip point ? +
? 2002 microchip technology inc. preliminary ds30485a-page 191 pic18fxx39 19.1 control register the low voltage detect control register controls the operation of the low voltage detect circuitry. register 19-1: lvdcon register u-0 u-0 r-0 r/w-0 r/w-0 r/w-1 r/w-0 r/w-1 ? ? irvst lvden lvdl3 lvdl2 lvdl1 lvdl0 bit 7 bit 0 bit 7-6 unimplemented: read as '0' bit 5 irvst: internal reference voltage stable flag bit 1 = indicates that the low voltage detect logic will generate the interrupt flag at the specified voltage range 0 = indicates that the low voltage detect logic will not generate the interrupt flag at the specified voltage range and the lvd interrupt should not be enabled bit 4 lvden: low voltage detect power enable bit 1 = enables lvd, powers up lvd circuit 0 = disables lvd, powers down lvd circuit bit 3-0 lvdl3:lvdl0: low voltage detection limit bits 1111 = external analog input is used (input comes from the lvdin pin) 1110 = 4.5v - 4.77v 1101 = 4.2v - 4.45v 1100 = 4.0v - 4.24v 1011 = 3.8v - 4.03v 1010 = 3.6v - 3.82v 1001 = 3.5v - 3.71v 1000 = 3.3v - 3.50v 0111 = 3.0v - 3.18v 0110 = 2.8v - 2.97v 0101 = 2.7v - 2.86v 0100 = 2.5v - 2.65v 0011 = 2.4v - 2.54v 0010 = 2.2v - 2.33v 0001 = 2.0v - 2.12v 0000 = reserved note: lvdl3:lvdl0 modes, which result in a trip point below the valid operating voltage of the device, are not tested. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18fxx39 ds30485a-page 192 preliminary ? 2002 microchip technology inc. 19.2 operation depending on the power source for the device voltage, the voltage normally decreases relatively slowly. this means that the lvd module does not need to be con- stantly operating. to decrease the current require- ments, the lvd circuitry only needs to be enabled for short periods, where the voltage is checked. after doing the check, the lvd module may be disabled. each time that the lvd module is enabled, the circuitry requires some time to stabilize. after the circuitry has stabilized, all status flags may be cleared. the module will then indicate the proper state of the system. the following steps are needed to set up the lvd module: 1. write the value to the lvdl3:lvdl0 bits (lvdcon register), which selects the desired lvd trip point. 2. ensure that lvd interrupts are disabled (the lvdie bit is cleared or the gie bit is cleared). 3. enable the lvd module (set the lvden bit in the lvdcon register). 4. wait for the lvd module to stabilize (the irvst bit to become set). 5. clear the lvd interrupt flag, which may have falsely become set until the lvd module has stabilized (clear the lvdif bit). 6. enable the lvd interrupt (set the lvdie and the gie bits). figure 19-4 shows typical waveforms that the lvd module may be used to detect. figure 19-4: low voltage detect waveforms v lvd v dd lvdif v lvd v dd enable lvd internally generated t ivrst lvdif may not be set enable lvd lvdif lvdif cleared in software lvdif cleared in software lvdif cleared in software, case 1: case 2: lvdif remains set since lvd condition still exists reference stable internally generated reference stable t ivrst
? 2002 microchip technology inc. preliminary ds30485a-page 193 pic18fxx39 19.2.1 reference voltage set point the internal reference voltage of the lvd module may be used by other internal circuitry (the programmable brown-out reset). if these circuits are disabled (lower current consumption), the reference voltage circuit requires a time to become stable before a low voltage condition can be reliably detected. this time is invariant of system clock speed. this start-up time is specified in electrical specification parameter 36. the low voltage interrupt flag will not be enabled until a stable reference voltage is reached. refer to the waveform in figure 19-4. 19.2.2 current consumption when the module is enabled, the lvd comparator and voltage divider are enabled and will consume static cur- rent. the voltage divider can be tapped from multiple places in the resistor array. total current consumption, when enabled, is specified in electrical specification parameter #d022b. 19.3 operation during sleep when enabled, the lvd circuitry continues to operate during sleep. if the device voltage crosses the trip point, the lvdif bit will be set and the device will wake- up from sleep. device execution will continue from the interrupt vector address if interrupts have been globally enabled. 19.4 effects of a reset a device reset forces all registers to their reset state. this forces the lvd module to be turned off.
pic18fxx39 ds30485a-page 194 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 195 pic18fxx39 20.0 special features of the cpu there are several features intended to maximize sys- tem reliability, minimize cost through elimination of external components, provide power saving operating modes and offer code protection. these are: ? osc selection ? reset - power-on reset (por) - power-up timer (pwrt) - oscillator start-up timer (ost) - brown-out reset (bor) ? interrupts ? watchdog timer (wdt) ? sleep ? code protection ? id locations ? in-circuit serial programming all pic18fxx39 devices have a watchdog timer, which is permanently enabled via the configuration bits, or software controlled. it runs off its own rc oscillator for added reliability. there are two timers that offer nec- essary delays on power-up. one is the oscillator start-up timer (ost), intended to keep the chip in reset until the crystal oscillator is stable. the other is the power-up timer (pwrt), which provides a fixed delay on power-up only, designed to keep the part in reset while the power supply stabilizes. with these two timers on-chip, most applications need no external reset circuitry. sleep mode is designed to offer a very low current power-down mode. the user can wake-up from sleep through external reset, watchdog timer wake-up or through an interrupt. several oscillator options are also made available to allow the part to fit the application. the rc oscillator option saves system cost, while the lp crystal option saves power. a set of configuration bits are used to select various options. 20.1 configuration bits the configuration bits can be programmed (read as '0'), or left unprogrammed (read as '1'), to select various device configurations. these bits are mapped starting at program memory location 300000h. the user will note that address 300000h is beyond the user program memory space. in fact, it belongs to the configuration memory space (300000h - 3fffffh), which can only be accessed using table reads and table writes. programming the configuration registers is done in a manner similar to programming the flash memory (see section 5.5.1). the only difference is the configu- ration registers are written a byte at a time. the sequence of events for programming configuration registers is: 1. load table pointer with address of configuration register being written. 2. write a single byte using the tblwt instruction. 3. set eepgd to point to program memory, set the cfgs bit to access configuration registers, and set wren to enable byte writes. 4. disable interrupts. 5. write 55h to eecon2. 6. write aah to eecon2. 7. set the wr bit. this will begin the write cycle. 8. cpu will stall for duration of write (approximately 2 ms using internal timer). 9. execute a nop . 10. re-enable interrupts.
pic18fxx39 ds30485a-page 196 preliminary ? 2002 microchip technology inc. table 20-1: configuration bits and device ids register 20-1: config1h: configuration register 1 high (byte address 300001h) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 default/ unprogrammed value 300001h config1h ? ? ? (1) ? ? fosc2 fosc1 fosc0 --1- -010 300002h config2l ? ? ? ? borv1 borv0 boren pwrten ---- 1111 300003h config2h ? ? ? ? wdtps2 wdtps1 wdtps0 wdten ---- 1111 300005h config3h ? ? ? ? ? ? ? ? (1) ---- ---1 300006h config4l debug ? ? ? ?lvp ?stvren 1--- -1-1 300008h config5l ? ? ? ? ? (1) cp2 cp1 cp0 ---- 1111 300009h config5h cpd cpb ? ? ? ? ? ? 11-- ---- 30000ah config6l ? ? ? ? ? (1) wrt2 wrt1 wrt0 ---- 1111 30000bh config6h wrtd wrtb wrtc ? ? ? ? ? 111- ---- 30000ch config7l ? ? ? ? ? (1) ebtr2 ebtr1 ebtr0 ---- 1111 30000dh config7h ?ebtrb ? ? ? ? ? ? -1-- ---- 3ffffeh devid1 dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 (2) 3fffffh devid2 dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 0000 0100 legend: x = unknown, u = unchanged, - = unimplemented. shaded cells are unimplemented, read as ?0?. note 1: unimplemented, but reserved; maintain this bit set. 2: see register 20-11 for devid1 values. u-0 u-0 u-1 u-0 u-0 r/p-0 r/p-1 r/p-0 ? ? ? ? ? fosc2 fosc1 fosc0 bit 7 bit 0 bit 7-6 unimplemented: read as ?0? bit 5 unimplemented and reserved: maintain as ?1? bit 4-3 unimplemented: read as ?0? bit 2-0 fosc2:fosc0 : oscillator selection bits 111 = reserved 110 = hs oscillator with pll enabled; clock frequency = (4 x f osc ) 101 = ec oscillator w/ osc2 configured as ra6 100 = ec oscillator w/ osc2 configured as divide-by-4 clock output 011 = reserved 010 = hs oscillator 001 = reserved 000 = reserved legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2002 microchip technology inc. preliminary ds30485a-page 197 pic18fxx39 register 20-2: config2l: configuration register 2 low (byte address 300002h) register 20-3: config2h: configuration register 2 high (byte address 300003h) u-0 u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ? ? borv1 borv0 boren pwrten bit 7 bit 0 bit 7-4 unimplemented: read as ?0? bit 3-2 borv1:borv0: brown-out reset voltage bits 11 = v bor set to 2.5v 10 = v bor set to 2.7v 01 = v bor set to 4.2v 00 = v bor set to 4.5v bit 1 boren: brown-out reset enable bit 1 = brown-out reset enabled 0 = brown-out reset disabled bit 0 pwrten : power-up timer enable bit 1 = pwrt disabled 0 = pwrt enabled legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state u-0 u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ? ? wdtps2 wdtps1 wdtps0 wdten bit 7 bit 0 bit 7-4 unimplemented: read as ?0? bit 3-1 wdtps2:wdtps0: watchdog timer postscale select bits 111 = 1:128 110 = 1:64 101 = 1:32 100 = 1:16 011 = 1:8 010 = 1:4 001 = 1:2 000 = 1:1 bit 0 wdten: watchdog timer enable bit 1 = wdt enabled 0 = wdt disabled (control is placed on the swdten bit) legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18fxx39 ds30485a-page 198 preliminary ? 2002 microchip technology inc. register 20-4: config4l: configuration register 4 low (byte address 300006h) r/p-1 u-0 u-0 u-0 u-0 r/p-1 u-0 r/p-1 debug ? ? ? ?lvp ?stvren bit 7 bit 0 bit 7 debug : background debugger enable bit 1 = background debugger disabled. rb6 and rb7 configured as general purpose i/o pins. 0 = background debugger enabled. rb6 and rb7 are dedicated to in-circuit debug. bit 6-3 unimplemented: read as ?0? bit 2 lvp: low voltage icsp enable bit 1 = low voltage icsp enabled 0 = low voltage icsp disabled bit 1 unimplemented: read as ?0? bit 0 stvren: stack full/underflow reset enable bit 1 = stack full/underflow will cause reset 0 = stack full/underflow will not cause reset legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2002 microchip technology inc. preliminary ds30485a-page 199 pic18fxx39 register 20-5: config5l: configuration register 5 low (byte address 300008h) register 20-6: config5h: configuration register 5 high (byte address 300009h) u-0 u-0 u-0 u-0 u-1 r/c-1 r/c-1 r/c-1 ? ? ? ? ?cp2 (1) cp1 cp0 bit 7 bit 0 bit 7-4 unimplemented: read as ?0? bit 3 unimplemented and reserved: maintain as ?1? bit 2 cp2: code protection bit (1) 1 = block 2 (004000-005fffh) not code protected 0 = block 2 (004000-005fffh) code protected bit 1 cp1: code protection bit 1 = block 1 (002000-003fffh) not code protected 0 = block 1 (002000-003fffh) code protected bit 0 cp0: code protection bit 1 = block 0 (000200-001fffh) not code protected 0 = block 0 (000200-001fffh) code protected note 1: unimplemented in pic18fx439 devices; maintain this bit set. legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state r/c-1 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 cpd cpb ? ? ? ? ? ? bit 7 bit 0 bit 7 cpd: data eeprom code protection bit 1 = data eeprom not code protected 0 = data eeprom code protected bit 6 cpb: boot block code protection bit 1 = boot block (000000-0001ffh) not code protected 0 = boot block (000000-0001ffh) code protected bit 5-0 unimplemented: read as ?0? legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18fxx39 ds30485a-page 200 preliminary ? 2002 microchip technology inc. register 20-7: config6l: configuration register 6 low (byte address 30000ah) register 20-8: config6h: configuration register 6 high (byte address 30000bh) u-0 u-0 u-0 u-0 u-1 r/c-1 r/c-1 r/c-1 ? ? ? ? ?wrt2 (1) wrt1 wrt0 bit 7 bit 0 bit 7-4 unimplemented: read as ?0? bit 3 unimplemented and reserved: maintain as ?1? bit 2 wrt2: write protection bit (1) 1 = block 2 (004000-005fffh) not write protected 0 = block 2 (004000-005fffh) write protected bit 1 wrt1: write protection bit 1 = block 1 (002000-003fffh) not write protected 0 = block 1 (002000-003fffh) write protected bit 0 wrt0: write protection bit 1 = block 0 (000200h-001fffh) not write protected 0 = block 0 (000200h-001fffh) write protected note 1: unimplemented in pic18fx439 devices; maintain this bit set. legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state r/c-1 r/c-1 c-1 u-0 u-0 u-0 u-0 u-0 wrtd wrtb wrtc ? ? ? ? ? bit 7 bit 0 bit 7 wrtd: data eeprom write protection bit 1 = data eeprom not write protected 0 = data eeprom write protected bit 6 wrtb: boot block write protection bit 1 = boot block (000000-0001ffh) not write protected 0 = boot block (000000-0001ffh) write protected bit 5 wrtc: configuration register write protection bit 1 = configuration registers (300000-3000ffh) not write protected 0 = configuration registers (300000-3000ffh) write protected note: this bit is read only, and cannot be changed in user mode. bit 4-0 unimplemented: read as ?0? legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2002 microchip technology inc. preliminary ds30485a-page 201 pic18fxx39 register 20-9: config7l: configuration register 7 low (byte address 30000ch) register 20-10: config7h: configuration register 7 high (byte address 30000dh) u-0 u-0 u-0 u-0 u-1 r/c-1 r/c-1 r/c-1 ? ? ? ? ? ebtr2 (1) ebtr1 ebtr0 bit 7 bit 0 bit 7-4 unimplemented: read as ?0? bit 3 unimplemented and reserved: maintain as ?1? bit 2 ebtr2: table read protection bit (1) 1 = block 2 (004000-005fffh) not protected from table reads executed in other blocks 0 = block 2 (004000-005fffh) protected from table reads executed in other blocks bit 1 ebtr1: table read protection bit 1 = block 1 (002000-003fffh) not protected from table reads executed in other blocks 0 = block 1 (002000-003fffh) protected from table reads executed in other blocks bit 0 ebtr0: table read protection bit 1 = block 0 (000200h-001fffh) not protected from table reads executed in other blocks 0 = block 0 (000200h-001fffh) protected from table reads executed in other blocks note 1: unimplemented in pic18fx439 devices; maintain this bit set. legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state u-0 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 ? ebtrb ? ? ? ? ? ? bit 7 bit 0 bit 7 unimplemented: read as ?0? bit 6 ebtrb: boot block table read protection bit 1 = boot block (000000-0001ffh) not protected from table reads executed in other blocks 0 = boot block (000000-0001ffh) protected from table reads executed in other blocks bit 5-0 unimplemented: read as ?0? legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18fxx39 ds30485a-page 202 preliminary ? 2002 microchip technology inc. register 20-11: devid1: device id register 1 for pic18fxx39 (byte address 3ffffeh) register 20-12: devid2: device id register 2 for pic18fxx39 (byte address 3fffffh) rrrrrrrr dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 bit 7 bit 0 bit 7-5 dev2:dev0: device id bits 000 = pic18f2539 001 = pic18F4539 100 = pic18f2439 101 = pic18f4439 bit 4-0 rev4:rev0: revision id bits these bits are used to indicate the device revision. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state rrrrrrrr dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 bit 7 bit 0 bit 7-0 dev10:dev3: device id bits these bits are used with the dev2:dev0 bits in the device id register 1 to identify the part number. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2002 microchip technology inc. preliminary ds30485a-page 203 pic18fxx39 20.2 watchdog timer (wdt) the watchdog timer is a free running on-chip rc oscillator, which does not require any external compo- nents. this rc oscillator is separate from the rc oscillator of the osc1/clki pin. that means that the wdt will run, even if the clock on the osc1/clki and osc2/clko/ra6 pins of the device has been stopped, for example, by execution of a sleep instruction. during normal operation, a wdt time-out generates a device reset (watchdog timer reset). if the device is in sleep mode, a wdt time-out causes the device to wake-up and continue with normal operation (watch- dog timer wake-up). the to bit in the rcon register will be cleared upon a wdt time-out. the watchdog timer is enabled/disabled by a device configuration bit. if the wdt is enabled, software exe- cution may not disable this function. when the wdten configuration bit is cleared, the swdten bit enables/ disables the operation of the wdt. the wdt time-out period values may be found in the electrical specifications (section 23.0) under parame- ter d031. values for the wdt postscaler may be assigned using the configuration bits. 20.2.1 control register register 20-13 shows the wdtcon register. this is a readable and writable register, which contains a control bit that allows software to override the wdt enable configuration bit, only when the configuration bit has disabled the wdt. register 20-13: wdtcon register note 1: the clrwdt and sleep instructions clear the wdt and the postscaler, if assigned to the wdt and prevent it from timing out and generating a device reset condition. 2: when a clrwdt instruction is executed and the postscaler is assigned to the wdt, the postscaler count will be cleared, but the postscaler assignment is not changed. u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0 ? ? ? ? ? ? ?swdten bit 7 bit 0 bit 7-1 unimplemented : read as ?0? bit 0 swdten: software controlled watchdog timer enable bit 1 = watchdog timer is on 0 = watchdog timer is turned off if the wdten configuration bit in the configuration register = 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por
pic18fxx39 ds30485a-page 204 preliminary ? 2002 microchip technology inc. 20.2.2 wdt postscaler the wdt has a postscaler that can extend the wdt reset period. the postscaler is selected at the time of the device programming, by the value written to the config2h configuration register. figure 20-1: watchdog timer block diagram table 20-2: summary of watchdog timer registers postscaler wdt timer wdten 8 - to - 1 mux wdtps2:wdtps0 wdt time-out 8 swdten bit configuration bit note: wdps2:wdps0 are bits in register config2h. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 config2h ? ? ? ? wdtps2 wdtps2 wdtps0 wdten rcon ipen ? ? ri to pd por bor wdtcon ? ? ? ? ? ? ?swdten legend: shaded cells are not used by the watchdog timer.
? 2002 microchip technology inc. preliminary ds30485a-page 205 pic18fxx39 20.3 power-down mode (sleep) power-down mode is entered by executing a sleep instruction. if enabled, the watchdog timer will be cleared, but keeps running, the pd bit (rcon<3>) is cleared, the to (rcon<4>) bit is set, and the oscillator driver is turned off. the i/o ports maintain the status they had before the sleep instruction was executed (driving high, low or hi-impedance). for lowest current consumption in this mode, place all i/o pins at either v dd or v ss , ensure no external cir- cuitry is drawing current from the i/o pin, power-down the a/d and disable external clocks. pull all i/o pins that are hi-impedance inputs, high or low externally, to avoid switching currents caused by floating inputs. the t0cki input should also be at v dd or v ss for lowest current consumption. the contribution from on-chip pull-ups on portb should be considered. the mclr pin must be at a logic high level (v ihmc ). 20.3.1 wake-up from sleep the device can wake-up from sleep through one of the following events: 1. external reset input on mclr pin. 2. watchdog timer wake-up (if wdt was enabled). 3. interrupt from int pin, rb port change or a peripheral interrupt. the following peripheral interrupts can wake the device from sleep: 1. psp read or write. 2. tmr1 interrupt. timer1 must be operating as an asynchronous counter. 3. tmr3 interrupt. timer3 must be operating as an asynchronous counter. 4. ccp capture mode interrupt. 5. special event trigger (timer1 in asynchronous mode using an external clock). 6. mssp (start/stop) bit detect interrupt. 7. mssp transmit or receive in slave mode (spi/i 2 c). 8. usart rx or tx (synchronous slave mode). 9. a/d conversion (when a/d clock source is rc). 10. eeprom write operation complete. 11. lvd interrupt. other peripherals cannot generate interrupts, since during sleep, no on-chip clocks are present. external mclr reset will cause a device reset. all other events are considered a continuation of program execution and will cause a ?wake-up?. the to and pd bits in the rcon register can be used to determine the cause of the device reset. the pd bit, which is set on power-up, is cleared when sleep is invoked. the to bit is cleared, if a wdt time-out occurred (and caused wake-up). when the sleep instruction is being executed, the next instruction (pc + 2) is pre-fetched. for the device to wake-up through an interrupt event, the corresponding interrupt enable bit must be set (enabled). wake-up is regardless of the state of the gie bit. if the gie bit is clear (disabled), the device continues execution at the instruction after the sleep instruction. if the gie bit is set (enabled), the device executes the instruction after the sleep instruction and then branches to the inter- rupt address. in cases where the execution of the instruction following sleep is not desirable, the user should have a nop after the sleep instruction. 20.3.2 wake-up using interrupts when global interrupts are disabled (gie cleared) and any interrupt source has both its interrupt enable bit and interrupt flag bit set, one of the following will occur: ? if an interrupt condition (interrupt flag bit and inter- rupt enable bits are set) occurs before the execu- tion of a sleep instruction, the sleep instruction will complete as a nop . therefore, the wdt and wdt postscaler will not be cleared, the to bit will not be set and pd bits will not be cleared. ? if the interrupt condition occurs during or after the execution of a sleep instruction, the device will immediately wake-up from sleep. the sleep instruction will be completely executed before the wake-up. therefore, the wdt and wdt postscaler will be cleared, the to bit will be set and the pd bit will be cleared. even if the flag bits were checked before executing a sleep instruction, it may be possible for flag bits to become set before the sleep instruction completes. to determine whether a sleep instruction executed, test the pd bit. if the pd bit is set, the sleep instruction was executed as a nop . to ensure that the wdt is cleared, a clrwdt instruction should be executed before a sleep instruction.
pic18fxx39 ds30485a-page 206 preliminary ? 2002 microchip technology inc. figure 20-2: wake-up from sleep through interrupt (1,2) 20.4 program verification and code protection the overall structure of the code protection on the pic18 flash devices differs significantly from other picmicro devices. the user program memory is divided on binary boundaries into individual blocks, each of which has three separate code protection bits associated with it: ? code protect bit (cpn) ? write protect bit (wrtn) ? external block table read bit (ebtrn) the code protection bits are located in configuration registers 5l through 7h. their locations within the registers are summarized in table 20-3. in the pic18fxx39 family, program memory is divided into segments of 8 kbytes. the first block in turn divided into a boot block of 512 bytes and a separately protected remainder (block 0) of 7.5 kbytes. this means for pic18fxx39 devices, that there may be up to five blocks, depending on the program memory size. the organization of the blocks and their associated code protection bits are shown in figure 20-3. for pic18fx439 devices, program memory is divided into three blocks: a boot block, block 0 (7.5 kbytes) and block 1 (8 kbytes). block 1 is further divided in half; the upper portion above 3000h is reserved, and unavailable to user applications. the entire block can be protected as a whole by bits cp1, wrt1 and ebtr1. by default, block 1 is not code protected. for pic18fx539 devices, program memory is divided into five blocks: the boot block, block 0 (7.5 kbytes), and blocks 1 through 3 (8 kbytes). code protection is implemented for the boot block and blocks 0 through 2. there is no provision for code protection for block 3. q1 q2 q3 q4 q1 q2 q3 q4 q1 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 clko (4) int pin intf flag (intcon<1>) gieh bit (intcon<7>) instruction flow pc instruction fetched instruction executed pc pc+2 pc+4 inst(pc) = sleep inst(pc - 1) inst(pc + 2) sleep processor in sleep interrupt latency (3) inst(pc + 4) inst(pc + 2) inst(0008h) inst(000ah) inst(0008h) dummy cycle pc + 4 0008h 000ah dummy cycle t ost (2) pc+4 note 1: xt, hs or lp oscillator mode assumed. 2: gie = 1 assumed. in this case, after wake-up, the processor jumps to the interrupt routine. if gie = 0, execution will continue in-line. 3: t ost = 1024 t osc (drawing not to scale). this delay will not occur for rc and ec osc modes. 4: clko is not available in these osc modes, but shown here for timing reference. note: the reserved segments of the program memory space are used by the motor con- trol kernel. for the kernel to function prop- erly, this area must not be write protected. if users are developing applications that require code protection for pic18fx439 devices, they should restrict program code (or at least those sections requiring protec- tion) to below the 1fffh memory boundary.
? 2002 microchip technology inc. preliminary ds30485a-page 207 pic18fxx39 figure 20-3: code protected program memory for pic18fxx39 table 20-3: summary of code protection registers memory size/device block code protection controlled by: 16 kbytes (pic18fx439) 32 kbytes (pic18fx539) address range boot block boot block 000000h 0001ffh cpb, wrtb, ebtrb block 0 block 0 000200h 001fffh cp0, wrt0, ebtr0 block 1 block 1 002000h 002fffh 003000h 003fffh cp1, wrt1, ebtr1 unimplemented read ?0?s block 2 004000h 005fffh cp2, wrt2, ebtr2 unimplemented read ?0?s 006000h 007fffh ? unimplemented read ?0?s unimplemented read ?0?s 008000h 1fffffh (unimplemented memory space) reserved reserved file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 300008h config5l ? ? ? ? ? (1) cp2 cp1 cp0 300009h config5h cpd cpb ? ? ? ? ? ? 30000ah config6l ? ? ? ? ? (1) wrt2 wrt1 wrt0 30000bh config6h wrtd wrtb wrtc ? ? ? ? ? 30000ch config7l ? ? ? ? ? (1) ebtr2 ebtr1 ebtr0 30000dh config7h ? ebtrb ? ? ? ? ? ? legend: shaded cells are unimplemented. note 1: unimplemented, but reserved; maintain this bit set.
pic18fxx39 ds30485a-page 208 preliminary ? 2002 microchip technology inc. 20.4.1 program memory code protection the user memory may be read to, or written from, any location using the table read and table write instruc- tions. the device id may be read with table reads. the configuration registers may be read and written with the table read and table write instructions. in user mode, the cpn bits have no direct effect. cpn bits inhibit external reads and writes. a block of user memory may be protected from table writes if the wrtn configuration bit is ?0?. the ebtrn bits control table reads. for a block of user memory with the ebtrn bit set to ?0?, a table read instruction that exe- cutes from within that block is allowed to read. a table read instruction that executes from a location outside of that block is not allowed to read, and will result in reading ?0?s. figures 20-4 through 20-6 illustrate table write and table read protection. figure 20-4: table write (wrtn) disallowed note: code protection bits may only be written to a ?0? from a ?1? state. it is not possible to write a ?1? to a bit in the ?0? state. code pro- tection bits are only set to ?1? by a block erase function. the block erase function can only be initiated via icsp or an external programmer. 000000h 0001ffh 000200h 001fffh 002000h 003fffh 004000h 005fffh wrtb,ebtrb = 11 wrt0,ebtr0 = 01 wrt1,ebtr1 = 11 wrt2,ebtr2 = 11 tblwt * tblptr = 000fff pc = 001ffe tblwt * pc = 004ffe register values program memory configuration bit settings results: all table writes disabled to blockn whenever wrtn = 0 .
? 2002 microchip technology inc. preliminary ds30485a-page 209 pic18fxx39 figure 20-5: external block table read (ebtrn) disallowed figure 20-6: external block table read (ebtrn) allowed 000000h 0001ffh 000200h 001fffh 002000h 003fffh 004000h 005fffh wrtb,ebtrb = 11 wrt0,ebtr0 = 10 wrt1,ebtr1 = 11 wrt2,ebtr2 = 11 tblrd * tblptr = 000fff pc = 002ffe results: all table reads from external blocks to blockn are disabled whenever ebtrn = 0. tablat register returns a value of ?0?. register values program memory configuration bit settings 000000h 0001ffh 000200h 001fffh 002000h 003fffh 004000h 005fffh wrtb,ebtrb = 11 wrt0,ebtr0 = 10 wrt1,ebtr1 = 11 wrt2,ebtr2 = 11 tblrd * tblptr = 000fff pc = 001ffe register values program memory configuration bit settings results: table reads permitted within blockn, even when ebtrbn = 0. tablat register returns the value of the data at the location tblptr.
pic18fxx39 ds30485a-page 210 preliminary ? 2002 microchip technology inc. 20.4.2 data eeprom code protection the entire data eeprom is protected from external reads and writes by two bits: cpd and wrtd. cpd inhibits external reads and writes of data eeprom. wrtd inhibits external writes to data eeprom. the cpu can continue to read and write data eeprom, regardless of the protection bit settings. 20.4.3 configuration register protection the configuration registers can be write protected. the wrtc bit controls protection of the configuration regis- ters. in user mode, the wrtc bit is readable only. wrtc can only be written via icsp or an external programmer. 20.5 id locations eight memory locations (200000h - 200007h) are des- ignated as id locations, where the user can store checksum or other code identification numbers. these locations are accessible during normal execution through the tblrd and tblwt instructions, or during program/verify. the id locations can be read when the device is code protected. the sequence for programming the id locations is sim- ilar to programming the flash memory (see section 5.5.1). 20.6 in-circuit serial programming pic18fxxx microcontrollers can be serially pro- grammed while in the end application circuit. this is simply done with two lines for clock and data, and three other lines for power, ground and the programming voltage. this allows customers to manufacture boards with unprogrammed devices, and then program the microcontroller just before shipping the product. this also allows the most recent firmware, or a custom firmware to be programmed. 20.7 in-circuit debugger when the debug bit in configuration register config4l is programmed to a '0', the in-circuit debugger functionality is enabled. this function allows simple debugging functions when used with mplab ? ide. when the microcontroller has this feature enabled, some of the resources are not available for general use. table 20-4 shows which features are consumed by the background debugger. table 20-4: debugger resources to use the in-circuit debugger function of the micro- controller, the design must implement in-circuit serial programming connections to mclr /v pp , v dd , gnd, rb7 and rb6. this will interface to the in-circuit debugger module available from microchip, or one of the third party development tool companies. 20.8 low voltage icsp programming the lvp bit configuration register config4l enables low voltage icsp programming. this mode allows the microcontroller to be programmed via icsp using a v dd source in the operating voltage range. this only means that v pp does not have to be brought to v ihh , but can instead be left at the normal operating voltage. in this mode, the rb5/pgm pin is dedicated to the pro- gramming function and ceases to be a general purpose i/o pin. during programming, v dd is applied to the mclr /v pp pin. to enter programming mode, v dd must be applied to the rb5/pgm, provided the lvp bit is set. the lvp bit defaults to a ? 1 ? from the factory. if low voltage programming mode is not used, the lvp bit can be programmed to a '0' and rb5/pgm becomes a digital i/o pin. however, the lvp bit may only be pro- grammed when programming is entered with v ihh on mclr /v pp . it should be noted that once the lvp bit is programmed to ?0?, only the high voltage programming mode is available and only high voltage programming mode can be used to program the device. when using low voltage icsp, the part must be sup- plied 4.5v to 5.5v, if a bulk erase will be executed. this includes reprogramming of the code protect bits from an on-state to an off-state. for all other cases of low voltage icsp, the part may be programmed at the nor- mal operating voltage. this means unique user ids, or user code can be reprogrammed or added. i/o pins rb6, rb7 stack 2 levels program memory 512 bytes data memory 10 bytes note 1: the high voltage programming mode is always available, regardless of the state of the lvp bit, by applying v ihh to the mclr pin. 2: while in low voltage icsp mode, the rb5 pin can no longer be used as a general purpose i/o pin, and should be held low during normal operation to protect against inadvertent icsp mode entry. 3: when using low voltage icsp program- ming (lvp), the pull-up on rb5 becomes disabled. if trisb bit 5 is cleared, thereby setting rb5 as an output, latb bit 5 must also be cleared for proper operation.
? 2002 microchip technology inc. preliminary ds30485a-page 211 pic18fxx39 21.0 instruction set summary the pic18fxxx instruction set adds many enhance- ments to the previous picmicro instruction sets, while maintaining an easy migration from these picmicro instruction sets. most instructions are a single program memory word (16-bits), but there are three instructions that require two program memory locations. each single word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction. the instruction set is highly orthogonal and is grouped into four basic categories: ? byte-oriented operations ? bit-oriented operations ? literal operations ? control operations the pic18fxxx instruction set summary in table 21-2 lists byte-oriented , bit-oriented , literal and control operations. table 21-1 shows the opcode field descriptions. most byte-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the destination of the result (specified by ?d?) 3. the accessed memory (specified by ?a?) the file register designator 'f' specifies which file register is to be used by the instruction. the destination designator ?d? specifies where the result of the operation is to be placed. if 'd' is zero, the result is placed in the wreg register. if 'd' is one, the result is placed in the file register specified in the instruction. all bit-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the bit in the file register (specified by ?b?) 3. the accessed memory (specified by ?a?) the bit field designator 'b' selects the number of the bit affected by the operation, while the file register desig- nator 'f' represents the number of the file in which the bit is located. the literal instructions may use some of the following operands: ? a literal value to be loaded into a file register (specified by ?k?) ? the desired fsr register to load the literal value into (specified by ?f?) ? no operand required (specified by ???) the control instructions may use some of the following operands: ? a program memory address (specified by ?n?) ? the mode of the call or return instructions (specified by ?s?) ? the mode of the table read and table write instructions (specified by ?m?) ? no operand required (specified by ???) all instructions are a single word, except for three dou- ble-word instructions. these three instructions were made double-word instructions, so that all the required information is available in these 32 bits. in the second word, the 4 msbs are ?1?s. if this second word is exe- cuted as an instruction (by itself), it will execute as a nop . all single word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruc- tion. in these cases, the execution takes two instruction cycles with the additional instruction cycle(s) executed as a nop . the double-word instructions execute in two instruction cycles. one instruction cycle consists of four oscillator periods. thus, for an oscillator frequency of 4 mhz, the normal instruction execution time is 1 s. if a conditional test is true, or the program counter is changed as a result of an instruction, the instruction execution time is 2 s. two-word branch instructions (if true) would take 3 s. figure 21-1 shows the general formats that the instructions can have. all examples use the format ? nnh ? to represent a hexa- decimal number, where ? h ? signifies a hexadecimal digit. the instruction set summary, shown in table 21-2, lists the instructions recognized by the microchip assembler (mpasm tm ). section 21.1 provides a description of each instruction.
pic18fxx39 ds30485a-page 212 preliminary ? 2002 microchip technology inc. table 21-1: opcode field descriptions field description a ram access bit a = 0: ram location in access ram (bsr register is ignored) a = 1: ram bank is specified by bsr register bbb bit address within an 8-bit file register (0 to 7). bsr bank select register. used to select the current ram bank. d destination select bit d = 0: store result in wreg, d = 1: store result in file register f. dest destination, either the wreg register or the specified register file location. f 8-bit register file address (0x00 to 0xff). fs 12-bit register file address (0x000 to 0xfff). this is the source address. fd 12-bit register file address (0x000 to 0xfff). this is the destination address. k literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value). label label name. mm the mode of the tblptr register for the table read and table write instructions. only used with table read and table write instructions: * no change to register (such as tblptr with table reads and writes). *+ post-increment register (such as tblptr with table reads and writes). *- post-decrement register (such as tblptr with table reads and writes). +* pre-increment register (such as tblptr with table reads and writes). n the relative address (2?s complement number) for relative branch instructions, or the direct address for call/branch and return instructions. prodh product of multiply high byte. prodl product of multiply low byte. s fast call/return mode select bit s = 0: do not update into/from shadow registers s = 1: certain registers loaded into/from shadow registers (fast mode) u unused or unchanged. wreg working register (accumulator). x don't care (0 or 1). the assembler will generate code with x = 0. it is the recommended form of use for compatibility with all microchip software tools. tblptr 21-bit table pointer (points to a program memory location). tablat 8-bit table latch. tos top-of-stack. pc program counter. pcl program counter low byte. pch program counter high byte. pclath program counter high byte latch. pclatu program counter upper byte latch. gie global interrupt enable bit. wdt watchdog timer. to time-out bit. pd power-down bit. c, dc, z, ov, n alu status bits carry, digit carry, zero, overflow, negative. [ ] optional. ( ) contents. assigned to. < > register bit field. in the set of. italics user defined term (font is courier).
? 2002 microchip technology inc. preliminary ds30485a-page 213 pic18fxx39 figure 21-1: general format for instructions byte-oriented file register operations 15 10 9 8 7 0 d = 0 for result destination to be wreg register opcode d a f (file #) d = 1 for result destination to be file register (f) a = 0 to force access bank bit-oriented file register operations 15 12 11 9 8 7 0 opcode b (bit #) a f (file #) b = 3-bit position of bit in file register (f) literal operations 15 8 7 0 opcode k (literal) k = 8-bit immediate value byte to byte move operations (2-word) 15 12 11 0 opcode f (source file #) call, goto and branch operations 15 8 7 0 opcode n<7:0> (literal) n = 20-bit immediate value a = 1 for bsr to select bank f = 8-bit file register address a = 0 to force access bank a = 1 for bsr to select bank f = 8-bit file register address 15 12 11 0 1111 n<19:8> (literal) 15 12 11 0 1111 f (destination file #) f = 12-bit file register address control operations example instruction addwf myreg, w, b movff myreg1, myreg2 bsf myreg, bit, b movlw 0x7f goto label 15 8 7 0 opcode n<7:0> (literal) 15 12 11 0 n<19:8> (literal) call myfunc 15 11 10 0 opcode n<10:0> (literal) s = fast bit bra myfunc 15 8 7 0 opcode n<7:0> (literal) bc myfunc s
pic18fxx39 ds30485a-page 214 preliminary ? 2002 microchip technology inc. table 21-2: pic18fxxx instruction set mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb byte-oriented file register operations addwf addwfc andwf clrf comf cpfseq cpfsgt cpfslt decf decfsz dcfsnz incf incfsz infsnz iorwf movf movff movwf mulwf negf rlcf rlncf rrcf rrncf setf subfwb subwf subwfb swapf tstfsz xorwf f, d, a f, d, a f, d, a f, a f, d, a f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f s , f d f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a add wreg and f add wreg and carry bit to f and wreg with f clear f complement f compare f with wreg, skip = compare f with wreg, skip > compare f with wreg, skip < decrement f decrement f, skip if 0 decrement f, skip if not 0 increment f increment f, skip if 0 increment f, skip if not 0 inclusive or wreg with f move f move f s (source) to 1st word f d (destination) 2nd word move wreg to f multiply wreg with f negate f rotate left f through carry rotate left f (no carry) rotate right f through carry rotate right f (no carry) set f subtract f from wreg with borrow subtract wreg from f subtract wreg from f with borrow swap nibbles in f test f, skip if 0 exclusive or wreg with f 1 1 1 1 1 1 (2 or 3) 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 (2 or 3) 1 0010 0010 0001 0110 0001 0110 0110 0110 0000 0010 0100 0010 0011 0100 0001 0101 1100 1111 0110 0000 0110 0011 0100 0011 0100 0110 0101 0101 0101 0011 0110 0001 01da0 0da 01da 101a 11da 001a 010a 000a 01da 11da 11da 10da 11da 10da 00da 00da ffff ffff 111a 001a 110a 01da 01da 00da 00da 100a 01da 11da 10da 10da 011a 10da ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff c, dc, z, ov, n c, dc, z, ov, n z, n z z, n none none none c, dc, z, ov, n none none c, dc, z, ov, n none none z, n z, n none none none c, dc, z, ov, n c, z, n z, n c, z, n z, n none c, dc, z, ov, n c, dc, z, ov, n c, dc, z, ov, n none none z, n 1, 2 1, 2 1,2 2 1, 2 4 4 1, 2 1, 2, 3, 4 1, 2, 3, 4 1, 2 1, 2, 3, 4 4 1, 2 1, 2 1 1, 2 1, 2 1, 2 1, 2 4 1, 2 bit-oriented file register operations bcf bsf btfsc btfss btg f, b, a f, b, a f, b, a f, b, a f, d, a bit clear f bit set f bit test f, skip if clear bit test f, skip if set bit toggle f 1 1 1 (2 or 3) 1 (2 or 3) 1 1001 1000 1011 1010 0111 bbba bbba bbba bbba bbba ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff none none none none none 1, 2 1, 2 3, 4 3, 4 1, 2 note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is '1' for a pin configured as input and is driven low by an external device, the data will be written back with a '0'. 2: if this instruction is executed on the tmr0 register (and, where applicable, d = 1), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are 2-word instructions. the second word of these instructions will be executed as a nop , unless the first word of the instruction retrieves the information embedded in these 16-bits. this ensures that all program memory locations have a valid instruction. 5: if the table write starts the write cycle to internal memory, the write will continue until terminated.
? 2002 microchip technology inc. preliminary ds30485a-page 215 pic18fxx39 control operations bc bn bnc bnn bnov bnz bov bra bz call clrwdt daw goto nop nop pop push rcall reset retfie retlw return sleep n n n n n n n n n n, s ? ? n ? ? ? ? n s k s ? branch if carry branch if negative branch if not carry branch if not negative branch if not overflow branch if not zero branch if overflow branch unconditionally branch if zero call subroutine 1st word 2nd word clear watchdog timer decimal adjust wreg go to address 1st word 2nd word no operation no operation pop top of return stack (tos) push top of return stack (tos) relative call software device reset return from interrupt enable return with literal in wreg return from subroutine go into standby mode 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 2 1 (2) 1 (2) 1 (2) 2 1 1 2 1 1 1 1 2 1 2 2 2 1 1110 1110 1110 1110 1110 1110 1110 1101 1110 1110 1111 0000 0000 1110 1111 0000 1111 0000 0000 1101 0000 0000 0000 0000 0000 0010 0110 0011 0111 0101 0001 0100 0nnn 0000 110s kkkk 0000 0000 1111 kkkk 0000 xxxx 0000 0000 1nnn 0000 0000 1100 0000 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0000 0000 kkkk kkkk 0000 xxxx 0000 0000 nnnn 1111 0001 kkkk 0001 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0100 0111 kkkk kkkk 0000 xxxx 0110 0101 nnnn 1111 000s kkkk 001s 0011 none none none none none none none none none none to , pd c none none none none none none all gie/gieh, peie/giel none none to , pd 4 table 21-2: pic18fxxx instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is '1' for a pin configured as input and is driven low by an external device, the data will be written back with a '0'. 2: if this instruction is executed on the tmr0 register (and, where applicable, d = 1), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are 2-word instructions. the second word of these instructions will be executed as a nop , unless the first word of the instruction retrieves the information embedded in these 16-bits. this ensures that all program memory locations have a valid instruction. 5: if the table write starts the write cycle to internal memory, the write will continue until terminated.
pic18fxx39 ds30485a-page 216 preliminary ? 2002 microchip technology inc. literal operations addlw andlw iorlw lfsr movlb movlw mullw retlw sublw xorlw k k k f, k k k k k k k add literal and wreg and literal with wreg inclusive or literal with wreg move literal (12-bit) 2nd word to fsrx 1st word move literal to bsr<3:0> move literal to wreg multiply literal with wreg return with literal in wreg subtract wreg from literal exclusive or literal with wreg 1 1 1 2 1 1 1 2 1 1 0000 0000 0000 1110 1111 0000 0000 0000 0000 0000 0000 1111 1011 1001 1110 0000 0001 1110 1101 1100 1000 1010 kkkk kkkk kkkk 00ff kkkk 0000 kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk c, dc, z, ov, n z, n z, n none none none none none c, dc, z, ov, n z, n data memory ? program memory operations tblrd* tblrd*+ tblrd*- tblrd+* tblwt* tblwt*+ tblwt*- tblwt+* table read table read with post-increment table read with post-decrement table read with pre-increment table write table write with post-increment table write with post-decrement table write with pre-increment 2 2 (5) 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 1001 1010 1011 1100 1101 1110 1111 none none none none none none none none table 21-2: pic18fxxx instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is '1' for a pin configured as input and is driven low by an external device, the data will be written back with a '0'. 2: if this instruction is executed on the tmr0 register (and, where applicable, d = 1), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are 2-word instructions. the second word of these instructions will be executed as a nop , unless the first word of the instruction retrieves the information embedded in these 16-bits. this ensures that all program memory locations have a valid instruction. 5: if the table write starts the write cycle to internal memory, the write will continue until terminated.
? 2002 microchip technology inc. preliminary ds30485a-page 217 pic18fxx39 21.1 instruction set addlw add literal to w syntax: [ label ] addlw k operands: 0 k 255 operation: (w) + k w status affected: n, ov, c, dc, z encoding: 0000 1111 kkkk kkkk description: the contents of w are added to the 8-bit literal 'k' and the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write to w example : addlw 0x15 before instruction w = 0x10 after instruction w = 0x25 addwf add w to f syntax: [ label ] addwf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (w) + (f) dest status affected: n, ov, c, dc, z encoding: 0010 01da ffff ffff description: add w to register 'f'. if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected. if ?a? is 1, the bsr is used. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : addwf reg, 0, 0 before instruction w = 0x17 reg = 0xc2 after instruction w=0xd9 reg = 0xc2
pic18fxx39 ds30485a-page 218 preliminary ? 2002 microchip technology inc. addwfc add w and carry bit to f syntax: [ label ] addwfc f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (w) + (f) + (c) dest status affected: n,ov, c, dc, z encoding: 0010 00da ffff ffff description: add w, the carry flag and data memory location 'f'. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed in data memory loca- tion 'f'. if ?a? is 0, the access bank will be selected. if ?a? is 1, the bsr will not be overridden. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : addwfc reg, 0, 1 before instruction carry bit = 1 reg = 0x02 w = 0x4d after instruction carry bit = 0 reg = 0x02 w = 0x50 andlw and literal with w syntax: [ label ] andlw k operands: 0 k 255 operation: (w) .and. k w status affected: n,z encoding: 0000 1011 kkkk kkkk description: the contents of w are anded with the 8-bit literal 'k'. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write to w example : andlw 0x5f before instruction w=0xa3 after instruction w = 0x03
? 2002 microchip technology inc. preliminary ds30485a-page 219 pic18fxx39 andwf and w with f syntax: [ label ] andwf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (w) .and. (f) dest status affected: n,z encoding: 0001 01da ffff ffff description: the contents of w are and?ed with register 'f'. if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected. if ?a? is 1, the bsr will not be overridden (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : andwf reg, 0, 0 before instruction w = 0x17 reg = 0xc2 after instruction w = 0x02 reg = 0xc2 bc branch if carry syntax: [ label ] bc n operands: -128 n 127 operation: if carry bit is ?1? (pc) + 2 + 2n pc status affected: none encoding: 1110 0010 nnnn nnnn description: if the carry bit is ?1?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bc 5 before instruction pc = address (here) after instruction if carry = 1; pc = address (here+12) if carry = 0; pc = address (here+2)
pic18fxx39 ds30485a-page 220 preliminary ? 2002 microchip technology inc. bcf bit clear f syntax: [ label ] bcf f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: 0 f status affected: none encoding: 1001 bbba ffff ffff description: bit 'b' in register 'f' is cleared. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : bcf flag_reg, 7, 0 before instruction flag_reg = 0xc7 after instruction flag_reg = 0x47 bn branch if negative syntax: [ label ] bn n operands: -128 n 127 operation: if negative bit is ?1? (pc) + 2 + 2n pc status affected: none encoding: 1110 0110 nnnn nnnn description: if the negative bit is ?1?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bn jump before instruction pc = address (here) after instruction if negative = 1; pc = address (jump) if negative = 0; pc = address (here+2)
? 2002 microchip technology inc. preliminary ds30485a-page 221 pic18fxx39 bnc branch if not carry syntax: [ label ] bnc n operands: -128 n 127 operation: if carry bit is ?0? (pc) + 2 + 2n pc status affected: none encoding: 1110 0011 nnnn nnnn description: if the carry bit is ?0?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bnc jump before instruction pc = address (here) after instruction if carry = 0; pc = address (jump) if carry = 1; pc = address (here+2) bnn branch if not negative syntax: [ label ] bnn n operands: -128 n 127 operation: if negative bit is ?0? (pc) + 2 + 2n pc status affected: none encoding: 1110 0111 nnnn nnnn description: if the negative bit is ?0?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bnn jump before instruction pc = address (here) after instruction if negative = 0; pc = address (jump) if negative = 1; pc = address (here+2)
pic18fxx39 ds30485a-page 222 preliminary ? 2002 microchip technology inc. bnov branch if not overflow syntax: [ label ] bnov n operands: -128 n 127 operation: if overflow bit is ?0? (pc) + 2 + 2n pc status affected: none encoding: 1110 0101 nnnn nnnn description: if the overflow bit is ?0?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bnov jump before instruction pc = address (here) after instruction if overflow = 0; pc = address (jump) if overflow = 1; pc = address (here+2) bnz branch if not zero syntax: [ label ] bnz n operands: -128 n 127 operation: if zero bit is ?0? (pc) + 2 + 2n pc status affected: none encoding: 1110 0001 nnnn nnnn description: if the zero bit is ?0?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bnz jump before instruction pc = address (here) after instruction if zero = 0; pc = address (jump) if zero = 1; pc = address (here+2)
? 2002 microchip technology inc. preliminary ds30485a-page 223 pic18fxx39 bra unconditional branch syntax: [ label ] bra n operands: -1024 n 1023 operation: (pc) + 2 + 2n pc status affected: none encoding: 1101 0nnn nnnn nnnn description: add the 2?s complement number ?2n? to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is a two-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation example : here bra jump before instruction pc = address (here) after instruction pc = address (jump) bsf bit set f syntax: [ label ] bsf f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: 1 f status affected: none encoding: 1000 bbba ffff ffff description: bit 'b' in register 'f' is set. if ?a? is 0 access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : bsf flag_reg, 7, 1 before instruction flag_reg = 0x0a after instruction flag_reg = 0x8a
pic18fxx39 ds30485a-page 224 preliminary ? 2002 microchip technology inc. btfsc bit test file, skip if clear syntax: [ label ] btfsc f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: skip if (f) = 0 status affected: none encoding: 1011 bbba ffff ffff description: if bit 'b' in register ?f' is 0, then the next instruction is skipped. if bit 'b' is 0, then the next instruction fetched during the current instruction execution is discarded, and a nop is executed instead, making this a two- cycle instruction. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfsc : : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (true) if flag<1> = 1; pc = address (false) btfss bit test file, skip if set syntax: [ label ] btfss f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: skip if (f) = 1 status affected: none encoding: 1010 bbba ffff ffff description: if bit 'b' in register 'f' is 1, then the next instruction is skipped. if bit 'b' is 1, then the next instruction fetched during the current instruc- tion execution, is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfss : : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (false) if flag<1> = 1; pc = address (true)
? 2002 microchip technology inc. preliminary ds30485a-page 225 pic18fxx39 btg bit toggle f syntax: [ label ] btg f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: (f ) f status affected: none encoding: 0111 bbba ffff ffff description: bit 'b' in data memory location 'f' is inverted. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : btg portc, 4, 0 before instruction: portc = 0111 0101 [0x75] after instruction: portc = 0110 0101 [0x65] bov branch if overflow syntax: [ label ] bov n operands: -128 n 127 operation: if overflow bit is ?1? (pc) + 2 + 2n pc status affected: none encoding: 1110 0100 nnnn nnnn description: if the overflow bit is ?1?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bov jump before instruction pc = address (here) after instruction if overflow = 1; pc = address (jump) if overflow = 0; pc = address (here+2)
pic18fxx39 ds30485a-page 226 preliminary ? 2002 microchip technology inc. bz branch if zero syntax: [ label ] bz n operands: -128 n 127 operation: if zero bit is ?1? (pc) + 2 + 2n pc status affected: none encoding: 1110 0000 nnnn nnnn description: if the zero bit is ?1?, then the pro- gram will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal 'n' process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal 'n' process data no operation example : here bz jump before instruction pc = address (here) after instruction if zero = 1; pc = address (jump) if zero = 0; pc = address (here+2) call subroutine call syntax: [ label ] call k [,s] operands: 0 k 1048575 s [0,1] operation: (pc) + 4 tos, k pc<20:1>, if s = 1 (w) ws, (status) statuss, (bsr) bsrs status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 110s k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: subroutine call of entire 2 mbyte memory range. first, return address (pc+ 4) is pushed onto the return stack. if ?s? = 1, the w, status and bsr registers are also pushed into their respective shadow registers, ws, statuss and bsrs. if 's' = 0, no update occurs (default). then, the 20-bit value ?k? is loaded into pc<20:1>. call is a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal 'k'<7:0>, push pc to stack read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : here call there,1 before instruction pc = address (here) after instruction pc = address (there) tos = address (here + 4) ws = w bsrs = bsr statuss= status
? 2002 microchip technology inc. preliminary ds30485a-page 227 pic18fxx39 clrf clear f syntax: [ label ] clrf f [,a] operands: 0 f 255 a [0,1] operation: 000h f 1 z status affected: z encoding: 0110 101a ffff ffff description: clears the contents of the specified register. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : clrf flag_reg,1 before instruction flag_reg = 0x5a after instruction flag_reg = 0x00 clrwdt clear watchdog timer syntax: [ label ] clrwdt operands: none operation: 000h wdt, 000h wdt postscaler, 1 to, 1 pd status affected: to , pd encoding: 0000 0000 0000 0100 description: clrwdt instruction resets the watchdog timer. it also resets the postscaler of the wdt. status bits to and pd are set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data no operation example : clrwdt before instruction wdt counter = ? after instruction wdt counter = 0x00 wdt postscaler = 0 to =1 pd =1
pic18fxx39 ds30485a-page 228 preliminary ? 2002 microchip technology inc. comf complement f syntax: [ label ] comf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: dest status affected: n, z encoding: 0001 11da ffff ffff description: the contents of register 'f' are com- plemented. if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : comf reg, 0, 0 before instruction reg = 0x13 after instruction reg = 0x13 w=0xec (f) cpfseq compare f with w, skip if f = w syntax: [ label ] cpfseq f [,a] operands: 0 f 255 a [0,1] operation: (f) ? (w), skip if (f) = (w) (unsigned comparison) status affected: none encoding: 0110 001a ffff ffff description: compares the contents of data memory location 'f' to the contents of w by performing an unsigned subtraction. if 'f' = w , then the fetched instruc- tion is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfseq reg, 0 nequal : equal : before instruction pc address = here w=? reg = ? after instruction if reg = w; pc = address (equal) if reg w; pc = address (nequal)
? 2002 microchip technology inc. preliminary ds30485a-page 229 pic18fxx39 cpfsgt compare f with w, skip if f > w syntax: [ label ] cpfsgt f [,a] operands: 0 f 255 a [0,1] operation: (f) ? ( w), skip if (f) > (w) (unsigned comparison) status affected: none encoding: 0110 010a ffff ffff description: compares the contents of data memory location 'f' to the contents of the w by performing an unsigned subtraction. if the contents of 'f' are greater than the contents of wreg , then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfsgt reg, 0 ngreater : greater : before instruction pc = address (here) w= ? after instruction if reg > w; pc = address (greater) if reg w; pc = address (ngreater) cpfslt compare f with w, skip if f < w syntax: [ label ] cpfslt f [,a] operands: 0 f 255 a [0,1] operation: (f) ? ( w), skip if (f) < (w) (unsigned comparison) status affected: none encoding: 0110 000a ffff ffff description: compares the contents of data memory location 'f' to the contents of w by performing an unsigned subtraction. if the contents of 'f' are less than the contents of w, then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0, the access bank will be selected. if ?a? is 1, the bsr will not be overridden (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfslt reg, 1 nless : less : before instruction pc = address (here) w= ? after instruction if reg < w; pc = address (less) if reg w; pc = address (nless)
pic18fxx39 ds30485a-page 230 preliminary ? 2002 microchip technology inc. daw decimal adjust w register syntax: [ label ] daw operands: none operation: if [w<3:0> >9] or [dc = 1] then (w<3:0>) + 6 w<3:0>; else ( w<3:0>) w<3:0>; if [w<7:4> >9] or [c = 1] then ( w<7:4>) + 6 w<7:4>; else (w<7:4>) w<7:4>; status affected: c encoding: 0000 0000 0000 0111 description: daw adjusts the eight-bit value in w, resulting from the earlier addi- tion of two variables (each in packed bcd format) and produces a correct packed bcd result. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register w process data write w example1 : daw before instruction w=0xa5 c=0 dc = 0 after instruction w = 0x05 c=1 dc = 0 example 2 : before instruction w=0xce c=0 dc = 0 after instruction w = 0x34 c=1 dc = 0 decf decrement f syntax: [ label ] decf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? 1 dest status affected: c, dc, n, ov, z encoding: 0000 01da ffff ffff description: decrement register 'f'. if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : decf cnt, 1, 0 before instruction cnt = 0x01 z=0 after instruction cnt = 0x00 z=1
? 2002 microchip technology inc. preliminary ds30485a-page 231 pic18fxx39 decfsz decrement f, skip if 0 syntax: [ label ] decfsz f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? 1 dest, skip if result = 0 status affected: none encoding: 0010 11da ffff ffff description: the contents of register 'f' are dec- remented. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if the result is 0, the next instruc- tion, which is already fetched, is discarded, and a nop is executed instead, making it a two-cycle instruction. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here decfsz cnt, 1, 1 goto loop continue before instruction pc = address (here) after instruction cnt = cnt - 1 if cnt = 0; pc = address (continue) if cnt 0; pc = address (here+2) dcfsnz decrement f, skip if not 0 syntax: [ label ] dcfsnz f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? 1 dest, skip if result 0 status affected: none encoding: 0100 11da ffff ffff description: the contents of register 'f' are dec- remented. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if the result is not 0, the next instruction, which is already fetched, is discarded, and a nop is executed instead, making it a two- cycle instruction. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here dcfsnz temp, 1, 0 zero : nzero : before instruction temp = ? after instruction temp = temp - 1, if temp = 0; pc = address (zero) if temp 0; pc = address (nzero)
pic18fxx39 ds30485a-page 232 preliminary ? 2002 microchip technology inc. goto unconditional branch syntax: [ label ] goto k operands: 0 k 1048575 operation: k pc<20:1> status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 1111 k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: goto allows an unconditional branch anywhere within entire 2 mbyte memory range. the 20-bit value ?k? is loaded into pc<20:1>. goto is always a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal 'k'<7:0>, no operation read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : goto there after instruction pc = address (there) incf increment f syntax: [ label ] incf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) + 1 dest status affected: c, dc, n, ov, z encoding: 0010 10da ffff ffff description: the contents of register 'f' are incremented. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : incf cnt, 1, 0 before instruction cnt = 0xff z=0 c=? dc = ? after instruction cnt = 0x00 z=1 c=1 dc = 1
? 2002 microchip technology inc. preliminary ds30485a-page 233 pic18fxx39 incfsz increment f, skip if 0 syntax: [ label ] incfsz f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) + 1 dest, skip if result = 0 status affected: none encoding: 0011 11da ffff ffff description: the contents of register 'f' are incremented. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f'. (default) if the result is 0, the next instruc- tion, which is already fetched, is discarded, and a nop is executed instead, making it a two-cycle instruction. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here incfsz cnt, 1, 0 nzero : zero : before instruction pc = address (here) after instruction cnt = cnt + 1 if cnt = 0; pc = address (zero) if cnt 0; pc = address (nzero) infsnz increment f, skip if not 0 syntax: [ label ] infsnz f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) + 1 dest, skip if result 0 status affected: none encoding: 0100 10da ffff ffff description: the contents of register 'f' are incremented. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if the result is not 0, the next instruction, which is already fetched, is discarded, and a nop is executed instead, making it a two- cycle instruction. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here infsnz reg, 1, 0 zero nzero before instruction pc = address (here) after instruction reg = reg + 1 if reg 0; pc = address (nzero) if reg = 0; pc = address (zero)
pic18fxx39 ds30485a-page 234 preliminary ? 2002 microchip technology inc. iorlw inclusive or literal with w syntax: [ label ] iorlw k operands: 0 k 255 operation: (w) .or. k w status affected: n, z encoding: 0000 1001 kkkk kkkk description: the contents of w are or?ed with the eight-bit literal 'k'. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write to w example : iorlw 0x35 before instruction w = 0x9a after instruction w=0xbf iorwf inclusive or w with f syntax: [ label ] iorwf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (w) .or. (f) dest status affected: n, z encoding: 0001 00da ffff ffff description: inclusive or w with register 'f'. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : iorwf result, 0, 1 before instruction result = 0x13 w = 0x91 after instruction result = 0x13 w = 0x93
? 2002 microchip technology inc. preliminary ds30485a-page 235 pic18fxx39 lfsr load fsr syntax: [ label ] lfsr f,k operands: 0 f 2 0 k 4095 operation: k fsrf status affected: none encoding: 1110 1111 1110 0000 00ff k 7 kkk k 11 kkk kkkk description: the 12-bit literal 'k' is loaded into the file select register pointed to by 'f'. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal 'k' msb process data write literal 'k' msb to fsrfh decode read literal 'k' lsb process data write literal 'k' to fsrfl example : lfsr 2, 0x3ab after instruction fsr2h = 0x03 fsr2l = 0xab movf move f syntax: [ label ] movf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: f dest status affected: n, z encoding: 0101 00da ffff ffff description: the contents of register 'f' are moved to a destination dependent upon the status of ?d?. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). location 'f' can be any- where in the 256 byte bank. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write w example : movf reg, 0, 0 before instruction reg = 0x22 w=0xff after instruction reg = 0x22 w = 0x22
pic18fxx39 ds30485a-page 236 preliminary ? 2002 microchip technology inc. movff move f to f syntax: [ label ] movff f s ,f d operands: 0 f s 4095 0 f d 4095 operation: (f s ) f d status affected: none encoding: 1st word (source) 2nd word (destin.) 1100 1111 ffff ffff ffff ffff fff f s fff f d description: the contents of source register 'f s ' are moved to destination register 'f d '. location of source 'f s ' can be anywhere in the 4096 byte data space (000h to fffh), and location of destination 'f d ' can also be any- where from 000h to fffh. either source or destination can be w (a useful special situation). movff is particularly useful for transferring a data memory location to a peripheral register (such as the transmit buffer or an i/o port). the movff instruction cannot use the pcl, tosu, tosh or tosl as the destination register. note: the movff instruction should not be used to mod- ify interrupt settings while any interrupt is enabled. see section 8.0 for more information. words: 2 cycles: 2 (3) q cycle activity: q1 q2 q3 q4 decode read register 'f' (src) process data no operation decode no operation no dummy read no operation write register 'f' (dest) example : movff reg1, reg2 before instruction reg1 = 0x33 reg2 = 0x11 after instruction reg1 = 0x33, reg2 = 0x33 movlb move literal to low nibble in bsr syntax: [ label ] movlb k operands: 0 k 255 operation: k bsr status affected: none encoding: 0000 0001 kkkk kkkk description: the 8-bit literal 'k' is loaded into the bank select register (bsr). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write literal 'k' to bsr example : movlb 5 before instruction bsr register = 0x02 after instruction bsr register = 0x05
? 2002 microchip technology inc. preliminary ds30485a-page 237 pic18fxx39 movlw move literal to w syntax: [ label ] movlw k operands: 0 k 255 operation: k w status affected: none encoding: 0000 1110 kkkk kkkk description: the eight-bit literal 'k' is loaded into w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write to w example : movlw 0x5a after instruction w = 0x5a movwf move w to f syntax: [ label ] movwf f [,a] operands: 0 f 255 a [0,1] operation: (w) f status affected: none encoding: 0110 111a ffff ffff description: move data from w to register 'f'. location 'f' can be anywhere in the 256 byte bank. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : movwf reg, 0 before instruction w = 0x4f reg = 0xff after instruction w = 0x4f reg = 0x4f
pic18fxx39 ds30485a-page 238 preliminary ? 2002 microchip technology inc. mullw multiply literal with w syntax: [ label ] mullw k operands: 0 k 255 operation: (w) x k prodh:prodl status affected: none encoding: 0000 1101 kkkk kkkk description: an unsigned multiplication is car- ried out between the contents of w and the 8-bit literal 'k'. the 16-bit result is placed in prodh:prodl register pair. prodh contains the high byte. w is unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this opera- tion. a zero result is possible but not detected. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write registers prodh: prodl example : mullw 0xc4 before instruction w=0xe2 prodh = ? prodl = ? after instruction w=0xe2 prodh = 0xad prodl = 0x08 mulwf multiply w with f syntax: [ label ] mulwf f [,a] operands: 0 f 255 a [0,1] operation: (w) x (f) prodh:prodl status affected: none encoding: 0000 001a ffff ffff description: an unsigned multiplication is car- ried out between the contents of w and the register file location 'f'. the 16-bit result is stored in the prodh:prodl register pair. prodh contains the high byte. both w and 'f' are unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this opera- tion. a zero result is possible but not detected. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write registers prodh: prodl example : mulwf reg, 1 before instruction w=0xc4 reg = 0xb5 prodh = ? prodl = ? after instruction w=0xc4 reg = 0xb5 prodh = 0x8a prodl = 0x94
? 2002 microchip technology inc. preliminary ds30485a-page 239 pic18fxx39 negf negate f syntax: [ label ] negf f [,a] operands: 0 f 255 a [0,1] operation: ( f ) + 1 f status affected: n, ov, c, dc, z encoding: 0110 110a ffff ffff description: location ?f? is negated using two?s complement. the result is placed in the data memory location 'f'. if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : negf reg, 1 before instruction reg = 0011 1010 [0x3a] after instruction reg = 1100 0110 [0xc6] nop no operation syntax: [ label ] nop operands: none operation: no operation status affected: none encoding: 0000 1111 0000 xxxx 0000 xxxx 0000 xxxx description: no operation. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation example : none.
pic18fxx39 ds30485a-page 240 preliminary ? 2002 microchip technology inc. pop pop top of return stack syntax: [ label ] pop operands: none operation: (tos) bit bucket status affected: none encoding: 0000 0000 0000 0110 description: the tos value is pulled off the return stack and is discarded. the tos value then becomes the previ- ous value that was pushed onto the return stack. this instruction is provided to enable the user to properly manage the return stack to incorporate a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation pop tos value no operation example : pop goto new before instruction tos = 0031a2h stack (1 level down) = 014332h after instruction tos = 014332h pc = new push push top of return stack syntax: [ label ] push operands: none operation: (pc+2) tos status affected: none encoding: 0000 0000 0000 0101 description: the pc+2 is pushed onto the top of the return stack. the previous tos value is pushed down on the stack. this instruction allows to implement a software stack by modifying tos, and then push it onto the return stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode push pc+2 onto return stack no operation no operation example : push before instruction tos = 00345ah pc = 000124h after instruction pc = 000126h tos = 000126h stack (1 level down) = 00345ah
? 2002 microchip technology inc. preliminary ds30485a-page 241 pic18fxx39 rcall relative call syntax: [ label ] rcall n operands: -1024 n 1023 operation: (pc) + 2 tos, (pc) + 2 + 2n pc status affected: none encoding: 1101 1nnn nnnn nnnn description: subroutine call with a jump up to 1k from the current location. first, return address (pc+2) is pushed onto the stack. then, add the 2?s complement number ?2n? to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is a two-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal 'n' push pc to stack process data write to pc no operation no operation no operation no operation example : here rcall jump before instruction pc = address (here) after instruction pc = address (jump) tos = address (here+2) reset reset syntax: [ label ] reset operands: none operation: reset all registers and flags that are affected by a mclr reset. status affected: all encoding: 0000 0000 1111 1111 description: this instruction provides a way to execute a mclr reset in software. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode start reset no operation no operation example : reset after instruction registers = reset value flags* = reset value
pic18fxx39 ds30485a-page 242 preliminary ? 2002 microchip technology inc. retfie return from interrupt syntax: [ label ] retfie [s] operands: s [0,1] operation: (tos) pc, 1 gie/gieh or peie/giel, if s = 1 (ws) w, (statuss) status, (bsrs) bsr, pclatu, pclath are unchanged. status affected: gie/gieh, peie/giel. encoding: 0000 0000 0001 000s description: return from interrupt. stack is popped and top-of-stack (tos) is loaded into the pc. interrupts are enabled by setting either the high or low priority global interrupt enable bit. if ?s? = 1, the contents of the shadow registers ws, statuss and bsrs are loaded into their corresponding registers, w, status and bsr. if ?s? = 0, no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation pop pc from stack set gieh or giel no operation no operation no operation no operation example : retfie 1 after interrupt pc = tos w=ws bsr = bsrs status = statuss gie/gieh, peie/giel = 1 retlw return literal to w syntax: [ label ] retlw k operands: 0 k 255 operation: k w, (tos) pc, pclatu, pclath are unchanged status affected: none encoding: 0000 1100 kkkk kkkk description: w is loaded with the eight-bit literal 'k'. the program counter is loaded from the top of the stack (the return address). the high address latch (pclath) remains unchanged. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data pop pc from stack, write to w no operation no operation no operation no operation example : call table ; w contains table ; offset value ; w now has ; table value : table addwf pcl ; w = offset retlw k0 ; begin table retlw k1 ; : : retlw kn ; end of table before instruction w = 0x07 after instruction w = value of kn
? 2002 microchip technology inc. preliminary ds30485a-page 243 pic18fxx39 return return from subroutine syntax: [ label ] return [s] operands: s [0,1] operation: (tos) pc, if s = 1 (ws) w, (statuss) status, (bsrs) bsr, pclatu, pclath are unchanged status affected: none encoding: 0000 0000 0001 001s description: return from subroutine. the stack is popped and the top of the stack (tos) is loaded into the program counter. if ?s?= 1, the contents of the shadow registers ws, statuss and bsrs are loaded into their cor- responding registers, w, status and bsr. if ?s? = 0, no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation process data pop pc from stack no operation no operation no operation no operation example : return after interrupt pc = tos rlcf rotate left f through carry syntax: [ label ] rlcf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<7>) c, (c) dest<0> status affected: c, n, z encoding: 0011 01da ffff ffff description: the contents of register 'f' are rotated one bit to the left through the carry flag. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? = 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : rlcf reg, 0, 0 before instruction reg = 1110 0110 c=0 after instruction reg = 1110 0110 w= 1100 1100 c=1 c register f
pic18fxx39 ds30485a-page 244 preliminary ? 2002 microchip technology inc. rlncf rotate left f (no carry) syntax: [ label ] rlncf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<7>) dest<0> status affected: n, z encoding: 0100 01da ffff ffff description: the contents of register 'f' are rotated one bit to the left. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : rlncf reg, 1, 0 before instruction reg = 1010 1011 after instruction reg = 0101 0111 register f rrcf rotate right f through carry syntax: [ label ] rrcf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<0>) c, (c) dest<7> status affected: c, n, z encoding: 0011 00da ffff ffff description: the contents of register 'f' are rotated one bit to the right through the carry flag. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : rrcf reg, 0, 0 before instruction reg = 1110 0110 c=0 after instruction reg = 1110 0110 w= 0111 0011 c=0 c register f
? 2002 microchip technology inc. preliminary ds30485a-page 245 pic18fxx39 rrncf rotate right f (no carry) syntax: [ label ] rrncf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<0>) dest<7> status affected: n, z encoding: 0100 00da ffff ffff description: the contents of register 'f' are rotated one bit to the right. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example 1 : rrncf reg, 1, 0 before instruction reg = 1101 0111 after instruction reg = 1110 1011 example 2 : rrncf reg, 0, 0 before instruction w=? reg = 1101 0111 after instruction w= 1110 1011 reg = 1101 0111 register f setf set f syntax: [ label ] setf f [,a] operands: 0 f 255 a [0,1] operation: ffh f status affected: none encoding: 0110 100a ffff ffff description: the contents of the specified regis- ter are set to ffh. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write register 'f' example : setf reg,1 before instruction reg = 0x5a after instruction reg = 0xff
pic18fxx39 ds30485a-page 246 preliminary ? 2002 microchip technology inc. sleep enter sleep mode syntax: [ label ] sleep operands: none operation: 00h wdt, 0 wdt postscaler, 1 to , 0 pd status affected: to , pd encoding: 0000 0000 0000 0011 description: the power-down status bit (pd ) is cleared. the time-out status bit (to ) is set. watchdog timer and its postscaler are cleared. the processor is put into sleep mode with the oscillator stopped. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data go to sleep example : sleep before instruction to =? pd =? after instruction to =1 ? pd =0 ? if wdt causes wake-up, this bit is cleared. subfwb subtract f from w with borrow syntax: [ label ] subfwb f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (w) ? (f) ? (c ) dest status affected: n, ov, c, dc, z encoding: 0101 01da ffff ffff description: subtract register 'f' and carry flag (borrow) from w (2?s complement method). if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example 1 : subfwb reg, 1, 0 before instruction reg = 3 w=2 c=1 after instruction reg = ff w=2 c=0 z=0 n = 1 ; result is negative example 2 : subfwb reg, 0, 0 before instruction reg = 2 w=5 c=1 after instruction reg = 2 w=3 c=1 z=0 n = 0 ; result is positive example 3 : subfwb reg, 1, 0 before instruction reg = 1 w=2 c=0 after instruction reg = 0 w=2 c=1 z = 1 ; result is zero n=0
? 2002 microchip technology inc. preliminary ds30485a-page 247 pic18fxx39 sublw subtract w from literal syntax: [ label ]sublw k operands: 0 k 255 operation: k ? (w) w status affected: n, ov, c, dc, z encoding: 0000 1000 kkkk kkkk description: w is subtracted from the eight-bit literal 'k'. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write to w example 1: sublw 0x02 before instruction w=1 c=? after instruction w=1 c = 1 ; result is positive z=0 n=0 example 2 : sublw 0x02 before instruction w=2 c=? after instruction w=0 c = 1 ; result is zero z=1 n=0 example 3 : sublw 0x02 before instruction w=3 c=? after instruction w = ff ; (2?s complement) c = 0 ; result is negative z=0 n=1 subwf subtract w from f syntax: [ label ] subwf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? (w) dest status affected: n, ov, c, dc, z encoding: 0101 11da ffff ffff description: subtract w from register 'f' (2?s complement method). if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in regis- ter 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example 1 : subwf reg, 1, 0 before instruction reg = 3 w=2 c=? after instruction reg = 1 w=2 c = 1 ; result is positive z=0 n=0 example 2 : subwf reg, 0, 0 before instruction reg = 2 w=2 c=? after instruction reg = 2 w=0 c = 1 ; result is zero z=1 n=0 example 3 : subwf reg, 1, 0 before instruction reg = 1 w=2 c=? after instruction reg = ffh ;(2?s complement) w=2 c = 0 ; result is negative z=0 n=1
pic18fxx39 ds30485a-page 248 preliminary ? 2002 microchip technology inc. subwfb subtract w from f with borrow syntax: [ label ] subwfb f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? (w) ? (c ) dest status affected: n, ov, c, dc, z encoding: 0101 10da ffff ffff description: subtract w and the carry flag (bor- row) from register 'f' (2?s complement method). if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example 1 : subwfb reg, 1, 0 before instruction reg = 0x19 (0001 1001) w = 0x0d (0000 1101) c=1 after instruction reg = 0x0c (0000 1011) w = 0x0d (0000 1101) c=1 z=0 n = 0 ; result is positive example 2 : subwfb reg, 0, 0 before instruction reg = 0x1b (0001 1011) w = 0x1a (0001 1010) c=0 after instruction reg = 0x1b (0001 1011) w = 0x00 c=1 z = 1 ; result is zero n=0 example 3: subwfb reg, 1, 0 before instruction reg = 0x03 (0000 0011) w = 0x0e (0000 1101) c=1 after instruction reg = 0xf5 (1111 0100) ; [2?s comp] w = 0x0e (0000 1101) c=0 z=0 n = 1 ; result is negative swapf swap f syntax: [ label ] swapf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (f<3:0>) dest<7:4>, (f<7:4>) dest<3:0> status affected: none encoding: 0011 10da ffff ffff description: the upper and lower nibbles of reg- ister 'f' are exchanged. if 'd' is 0, the result is placed in w. if 'd' is 1, the result is placed in register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : swapf reg, 1, 0 before instruction reg = 0x53 after instruction reg = 0x35
? 2002 microchip technology inc. preliminary ds30485a-page 249 pic18fxx39 tblrd table read syntax: [ label ] tblrd ( *; *+; *-; +*) operands: none operation: if tblrd *, (prog mem (tblptr)) tablat; tblptr - no change; if tblrd *+, (prog mem (tblptr)) tablat; (tblptr) +1 tblptr; if tblrd *-, (prog mem (tblptr)) tablat; (tblptr) -1 tblptr; if tblrd +*, (tblptr) +1 tblptr; (prog mem (tblptr)) tablat; status affected:none encoding: 0000 0000 0000 10nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction is used to read the con- tents of program memory (p.m.). to address the program memory, a pointer called table pointer (tblptr) is used. the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2 mbyte address range. tblptr[0] = 0: least significant byte of program memory word tblptr[0] = 1: most significant byte of program memory word the tblrd instruction can modify the value of tblptr as follows: ? no change ? post-increment ? post-decrement ? pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read program memory) no operation no operation (write tablat) tblrd table read (cont?d) example1 : tblrd *+ ; before instruction tablat = 0x55 tblptr = 0x00a356 memory(0x00a356) = 0x34 after instruction tablat = 0x34 tblptr = 0x00a357 example2 : tblrd +* ; before instruction tablat = 0xaa tblptr = 0x01a357 memory(0x01a357) = 0x12 memory(0x01a358) = 0x34 after instruction tablat = 0x34 tblptr = 0x01a358
pic18fxx39 ds30485a-page 250 preliminary ? 2002 microchip technology inc. tblwt table write syntax: [ label ] tblwt ( *; *+; *-; +*) operands: none operation: if tblwt*, (tablat) holding register; tblptr - no change; if tblwt*+, (tablat) holding register; (tblptr) +1 tblptr; if tblwt*-, (tablat) holding register; (tblptr) -1 tblptr; if tblwt+*, (tblptr) +1 tblptr; (tablat) holding register; status affected: none encoding: 0000 0000 0000 11nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction uses the 3 lsbs of the tblptr to determine which of the 8 holding registers the tablat data is written to. the 8 holding registers are used to program the contents of pro- gram memory (p.m.). see section 5.0 for information on writing to flash memory. the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2 mbtye address range. the lsb of the tblptr selects which byte of the program memory location to access. tblptr[0] = 0: least significant byte of program memory word tblptr[0] = 1: most significant byte of program memory word the tblwt instruction can modify the value of tblptr as follows: ? no change ? post-increment ? post-decrement ? pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read tablat) no operation no operation (write to holding register or memory) tblwt table write (continued) example1 : tblwt *+; before instruction tablat = 0x55 tblptr = 0x00a356 holding register (0x00a356) = 0xff after instructions (table write completion) tablat = 0x55 tblptr = 0x00a357 holding register (0x00a356) = 0x55 example 2 : tblwt +*; before instruction tablat = 0x34 tblptr = 0x01389a holding register (0x01389a) = 0xff holding register (0x01389b) = 0xff after instruction (table write completion) tablat = 0x34 tblptr = 0x01389b holding register (0x01389a) = 0xff holding register (0x01389b) = 0x34
? 2002 microchip technology inc. preliminary ds30485a-page 251 pic18fxx39 tstfsz test f, skip if 0 syntax: [ label ] tstfsz f [,a] operands: 0 f 255 a [0,1] operation: skip if f = 0 status affected: none encoding: 0110 011a ffff ffff description: if 'f' = 0, the next instruction, fetched during the current instruc- tion execution, is discarded and a nop is executed, making this a two- cycle instruction. if ?a? is 0, the access bank will be selected, over- riding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register 'f' process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here tstfsz cnt, 1 nzero : zero : before instruction pc = address (here) after instruction if cnt = 0x00, pc = address (zero) if cnt 0x00, pc = address (nzero) xorlw exclusive or literal with w syntax: [ label ] xorlw k operands: 0 k 255 operation: (w) .xor. k w status affected: n, z encoding: 0000 1010 kkkk kkkk description: the contents of w are xored with the 8-bit literal 'k'. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal 'k' process data write to w example : xorlw 0xaf before instruction w=0xb5 after instruction w = 0x1a
pic18fxx39 ds30485a-page 252 preliminary ? 2002 microchip technology inc. xorwf exclusive or w with f syntax: [ label ] xorwf f [,d [,a] operands: 0 f 255 d [0,1] a [0,1] operation: (w) .xor. (f) dest status affected: n, z encoding: 0001 10da ffff ffff description: exclusive or the contents of w with register 'f'. if 'd' is 0, the result is stored in w. if 'd' is 1, the result is stored back in the register 'f' (default). if ?a? is 0, the access bank will be selected, overriding the bsr value. if ?a? is 1, then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : xorwf reg, 1, 0 before instruction reg = 0xaf w=0xb5 after instruction reg = 0x1a w=0xb5
? 2002 microchip technology inc. preliminary ds30485a-page 253 pic18fxx39 22.0 development support the picmicro ? microcontrollers are supported with a full range of hardware and software development tools: ? integrated development environment - mplab ? ide software ? assemblers/compilers/linkers - mpasm tm assembler - mplab c17 and mplab c18 c compilers -mplink tm object linker/ mplib tm object librarian ? simulators - mplab sim software simulator ?emulators - mplab ice 2000 in-circuit emulator - icepic? in-circuit emulator ? in-circuit debugger - mplab icd ? device programmers -pro mate ? ii universal device programmer - picstart ? plus entry-level development programmer ? low cost demonstration boards - picdem tm 1 demonstration board - picdem 2 demonstration board - picdem 3 demonstration board - picdem 17 demonstration board -k ee l oq ? demonstration board 22.1 mplab integrated development environment software the mplab ide software brings an ease of software development previously unseen in the 8-bit microcon- troller market. the mplab ide is a windows ? based application that contains: ? an interface to debugging tools - simulator - programmer (sold separately) - emulator (sold separately) - in-circuit debugger (sold separately) ? a full-featured editor ? a project manager ? customizable toolbar and key mapping ? a status bar ? on-line help the mplab ide allows you to: ? edit your source files (either assembly or ?c?) ? one touch assemble (or compile) and download to picmicro emulator and simulator tools (auto- matically updates all project information) ? debug using: - source files - absolute listing file - machine code the ability to use mplab ide with multiple debugging tools allows users to easily switch from the cost- effective simulator to a full-featured emulator with minimal retraining. 22.2 mpasm assembler the mpasm assembler is a full-featured universal macro assembler for all picmicro mcu?s. the mpasm assembler has a command line interface and a windows shell. it can be used as a stand-alone application on a windows 3.x or greater system, or it can be used through mplab ide. the mpasm assem- bler generates relocatable object files for the mplink object linker, intel ? standard hex files, map files to detail memory usage and symbol reference, an abso- lute lst file that contains source lines and generated machine code, and a cod file for debugging. the mpasm assembler features include: ? integration into mplab ide projects. ? user-defined macros to streamline assembly code. ? conditional assembly for multi-purpose source files. ? directives that allow complete control over the assembly process. 22.3 mplab c17 and mplab c18 c compilers the mplab c17 and mplab c18 code development systems are complete ansi ?c? compilers for microchip?s pic17cxxx and pic18cxxx family of microcontrollers, respectively. these compilers provide powerful integration capabilities and ease of use not found with other compilers. for easier source level debugging, the compilers pro- vide symbol information that is compatible with the mplab ide memory display.
pic18fxx39 ds30485a-page 254 preliminary ? 2002 microchip technology inc. 22.4 mplink object linker/ mplib object librarian the mplink object linker combines relocatable objects created by the mpasm assembler and the mplab c17 and mplab c18 c compilers. it can also link relocatable objects from pre-compiled libraries, using directives from a linker script. the mplib object librarian is a librarian for pre- compiled code to be used with the mplink object linker. when a routine from a library is called from another source file, only the modules that contain that routine will be linked in with the application. this allows large libraries to be used efficiently in many different applications. the mplib object librarian manages the creation and modification of library files. the mplink object linker features include: ? integration with mpasm assembler and mplab c17 and mplab c18 c compilers. ? allows all memory areas to be defined as sections to provide link-time flexibility. the mplib object librarian features include: ? easier linking because single libraries can be included instead of many smaller files. ? helps keep code maintainable by grouping related modules together. ? allows libraries to be created and modules to be added, listed, replaced, deleted or extracted. 22.5 mplab sim software simulator the mplab sim software simulator allows code devel- opment in a pc-hosted environment by simulating the picmicro series microcontrollers on an instruction level. on any given instruction, the data areas can be examined or modified and stimuli can be applied from a file, or user-defined key press, to any of the pins. the execution can be performed in single step, execute until break, or trace mode. the mplab sim simulator fully supports symbolic debug- ging using the mplab c17 and the mplab c18 c com- pilers and the mpasm assembler. the software simulator offers the flexibility to develop and debug code outside of the laboratory environment, making it an excellent multi- project software development tool. 22.6 mplab ice high performance universal in-circuit emulator with mplab ide the mplab ice universal in-circuit emulator is intended to provide the product development engineer with a complete microcontroller design tool set for picmicro microcontrollers (mcus). software control of the mplab ice in-circuit emulator is provided by the mplab integrated development environment (ide), which allows editing, building, downloading and source debugging from a single environment. the mplab ice 2000 is a full-featured emulator sys- tem with enhanced trace, trigger and data monitoring features. interchangeable processor modules allow the system to be easily reconfigured for emulation of differ- ent processors. the universal architecture of the mplab ice in-circuit emulator allows expansion to support new picmicro microcontrollers. the mplab ice in-circuit emulator system has been designed as a real-time emulation system, with advanced features that are generally found on more expensive development tools. the pc platform and microsoft ? windows environment were chosen to best make these features available to you, the end user. 22.7 icepic in-circuit emulator the icepic low cost, in-circuit emulator is a solution for the microchip technology pic16c5x, pic16c6x, pic16c7x and pic16cxxx families of 8-bit one- time-programmable (otp) microcontrollers. the mod- ular system can support different subsets of pic16c5x or pic16cxxx products through the use of inter- changeable personality modules, or daughter boards. the emulator is capable of emulating without target application circuitry being present.
? 2002 microchip technology inc. preliminary ds30485a-page 255 pic18fxx39 22.8 mplab icd in-circuit debugger microchip's in-circuit debugger, mplab icd, is a pow- erful, low cost, run-time development tool. this tool is based on the flash picmicro mcus and can be used to develop for this and other picmicro microcontrollers. the mplab icd utilizes the in-circuit debugging capa- bility built into the flash devices. this feature, along with microchip's in-circuit serial programming tm proto- col, offers cost-effective in-circuit flash debugging from the graphical user interface of the mplab integrated development environment. this enables a designer to develop and debug source code by watch- ing variables, single-stepping and setting break points. running at full speed enables testing hardware in real- time. 22.9 pro mate ii universal device programmer the pro mate ii universal device programmer is a full-featured programmer, capable of operating in stand-alone mode, as well as pc-hosted mode. the pro mate ii device programmer is ce compliant. the pro mate ii device programmer has program- mable v dd and v pp supplies, which allow it to verify programmed memory at v dd min and v dd max for max- imum reliability. it has an lcd display for instructions and error messages, keys to enter commands and a modular detachable socket assembly to support various package types. in stand-alone mode, the pro mate ii device programmer can read, verify, or program picmicro devices. it can also set code protection in this mode. 22.10 picstart plus entry level development programmer the picstart plus development programmer is an easy-to-use, low cost, prototype programmer. it con- nects to the pc via a com (rs-232) port. mplab integrated development environment software makes using the programmer simple and efficient. the picstart plus development programmer sup- ports all picmicro devices with up to 40 pins. larger pin count devices, such as the pic16c92x and pic17c76x, may be supported with an adapter socket. the picstart plus development programmer is ce compliant. 22.11 picdem 1 low cost picmicro demonstration board the picdem 1 demonstration board is a simple board which demonstrates the capabilities of several of microchip?s microcontrollers. the microcontrollers sup- ported are: pic16c5x (pic16c54 to pic16c58a), pic16c61, pic16c62x, pic16c71, pic16c8x, pic17c42, pic17c43 and pic17c44. all necessary hardware and software is included to run basic demo programs. the user can program the sample microcon- trollers provided with the picdem 1 demonstration board on a pro mate ii device programmer, or a picstart plus development programmer, and easily test firmware. the user can also connect the picdem 1 demonstration board to the mplab ice in- circuit emulator and download the firmware to the emu- lator for testing. a prototype area is available for the user to build some additional hardware and connect it to the microcontroller socket(s). some of the features include an rs-232 interface, a potentiometer for simu- lated analog input, push button switches and eight leds connected to portb. 22.12 picdem 2 low cost pic16cxx demonstration board the picdem 2 demonstration board is a simple dem- onstration board that supports the pic16c62, pic16c64, pic16c65, pic16c73 and pic16c74 microcontrollers. all the necessary hardware and soft- ware is included to run the basic demonstration pro- grams. the user can program the sample microcontrollers provided with the picdem 2 demon- stration board on a pro mate ii device programmer, or a picstart plus development programmer, and easily test firmware. the mplab ice in-circuit emula- tor may also be used with the picdem 2 demonstration board to test firmware. a prototype area has been pro- vided to the user for adding additional hardware and connecting it to the microcontroller socket(s). some of the features include a rs-232 interface, push button switches, a potentiometer for simulated analog input, a serial eeprom to demonstrate usage of the i 2 c tm bus and separate headers for connection to an lcd module and a keypad.
pic18fxx39 ds30485a-page 256 preliminary ? 2002 microchip technology inc. 22.13 picdem 3 low cost pic16cxxx demonstration board the picdem 3 demonstration board is a simple dem- onstration board that supports the pic16c923 and pic16c924 in the plcc package. it will also support future 44-pin plcc microcontrollers with an lcd mod- ule. all the necessary hardware and software is included to run the basic demonstration programs. the user can program the sample microcontrollers pro- vided with the picdem 3 demonstration board on a pro mate ii device programmer, or a picstart plus development programmer with an adapter socket, and easily test firmware. the mplab ice in-circuit emula- tor may also be used with the picdem 3 demonstration board to test firmware. a prototype area has been pro- vided to the user for adding hardware and connecting it to the microcontroller socket(s). some of the features include a rs-232 interface, push button switches, a potentiometer for simulated analog input, a thermistor and separate headers for connection to an external lcd module and a keypad. also provided on the picdem 3 demonstration board is a lcd panel, with 4 commons and 12 segments, that is capable of display- ing time, temperature and day of the week. the picdem 3 demonstration board provides an additional rs-232 interface and windows software for showing the demultiplexed lcd signals on a pc. a simple serial interface allows the user to construct a hardware demultiplexer for the lcd signals. 22.14 picdem 17 demonstration board the picdem 17 demonstration board is an evaluation board that demonstrates the capabilities of several microchip microcontrollers, including pic17c752, pic17c756a, pic17c762 and pic17c766. all neces- sary hardware is included to run basic demo programs, which are supplied on a 3.5-inch disk. a programmed sample is included and the user may erase it and program it with the other sample programs using the pro mate ii device programmer, or the picstart plus development programmer, and easily debug and test the sample code. in addition, the picdem 17 dem- onstration board supports downloading of programs to and executing out of external flash memory on board. the picdem 17 demonstration board is also usable with the mplab ice in-circuit emulator, or the picmaster emulator and all of the sample programs can be run and modified using either emulator. addition- ally, a generous prototype area is available for user hardware. 22.15 k ee l oq evaluation and programming tools k ee l oq evaluation and programming tools support microchip?s hcs secure data products. the hcs eval- uation kit includes a lcd display to show changing codes, a decoder to decode transmissions and a pro- gramming interface to program test transmitters.
? 2002 microchip technology inc. preliminary ds30485a-page 257 pic18fxx39 table 22-1: development tools from microchip pic12cxxx pic14000 pic16c5x pic16c6x pic16cxxx pic16f62x pic16c7x pic16c7xx pic16c8x/ pic16f8x pic16f8xx pic16c9xx pic17c4x pic17c7xx pic18cxx2 pic18fxxx 24cxx/ 25cxx/ 93cxx hcsxxx mcrfxxx mcp2510 software tools mplab ? integrated development environment 999999999999999 mplab ? c17 c compiler 99 mplab ? c18 c compiler 99 mpasm tm assembler/ mplink tm object linker 999999999999999 9 9 emulators mplab ? ice in-circuit emulator 999 9 99 ** 999999999 icepic tm in-circuit emulator 9 999 999 9 debugger mplab ? icd in-circuit debugger 9 * 9 * 99 programmers picstart ? plus entry level development programmer 999 9 99 ** 999999999 pro mate ? ii universal device programmer 999 9 99 ** 999999999 9 9 demo boards and eval kits picdem tm 1 demonstration board 999 ? 99 picdem tm 2 demonstration board 9 ? 9 ? 99 picdem tm 3 demonstration board 9 picdem tm 14a demonstration board 9 picdem tm 17 demonstration board 9 k ee l oq ? evaluation kit 9 k ee l oq ? transponder kit 9 microid tm programmer?s kit 9 125 khz microid tm developer?s kit 9 125 khz anticollision microid tm developer?s kit 9 13.56 mhz anticollision microid tm developer?s kit 9 mcp2510 can developer?s kit 9 * contact the microchip technology inc. web site at www.microchip.com for information on how to use the mplab ? icd in-circuit debugger (dv164001) with pic16c62, 63, 64, 65, 72, 73, 74, 76, 77. ** contact microchip technology inc. for availability date. ? development tool is available on select devices.
pic18fxx39 ds30485a-page 258 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 259 pic18fxx39 23.0 electrical characteristics absolute maximum ratings (?) ambient temperature under bias................................................................................................. ............-55c to +125c storage temperature ............................................................................................................ .................. -65c to +150c voltage on any pin with respect to v ss (except v dd , mclr , and ra4) ......................................... -0.3v to (v dd + 0.3v) voltage on v dd with respect to v ss ......................................................................................................... -0.3v to +7.5v voltage on mclr with respect to v ss (note 2) ......................................................................................... 0v to +13.25v voltage on ra4 with respect to vss ............................................................................................. .................. 0v to +8.5v total power dissipation (note 1) ............................................................................................................................... 1.0w maximum current out of v ss pin ........................................................................................................................... 300 ma maximum current into v dd pin ........................................................................................................................... ...250 ma input clamp current, i ik (v i < 0 or v i > v dd ) ............................................................... ....................................................... 20 ma output clamp current, i ok (v o < 0 or v o > v dd ) ............................................................... ............................................... 20 ma maximum output current sunk by any i/o pin..................................................................................... .....................25 ma maximum output current sourced by any i/o pin .................................................................................. ..................25 ma maximum current sunk by porta, portb, and porte (note 3) (combined) ...................................................200 ma maximum current sourced by porta, portb, and porte (note 3) (combined)..............................................200 ma maximum current sunk by portc and portd (note 3) (combined)..................................................................200 ma maximum current sourced by portc and portd (note 3) (combined).............................................................200 ma note 1: power dissipation is calculated as follows: pdis = v dd x {i dd - i oh } + {(v dd -v oh ) x i oh } + (v o l x i ol ) 2: voltage spikes below v ss at the mclr /v pp pin, inducing currents greater than 80 ma, may cause latchup. thus, a series resistor of 50-100 ? should be used when applying a ?low? level to the mclr /v pp pin, rather than pulling this pin directly to v ss . 3: portd and porte not available on the pic18f2x39 devices. ? notice: stresses above those listed under ?absolute maximum ratings? may cause permanent damage to the device. this is a stress rating only and functional operation of the device at those or any other conditions above those indicated in the operation listings of this specification is not implied. exposure to maximum rating conditions for extended periods may affect device reliability.
pic18fxx39 ds30485a-page 260 preliminary ? 2002 microchip technology inc. figure 23-1: pic18fxx39 voltage-frequency graph (industrial) figure 23-2: pic18lfxx39 voltage-frequency graph (industrial) frequency voltage 6.0v 5.5v 4.5v 4.0v 2.0v 40 mhz 5.0v 3.5v 3.0v 2.5v pic18fxx39 4.2v frequency voltage 6.0v 5.5v 4.5v 4.0v 2.0v 40 mhz 5.0v 3.5v 3.0v 2.5v pic18lfxx39 f max = (16.36 mhz/v) (v ddappmin ? 2.0v) + 4 mhz note: v ddappmin is the minimum voltage of the picmicro ? device in the application. 4 mhz 4.2v
? 2002 microchip technology inc. preliminary ds30485a-page 261 pic18fxx39 23.1 dc characteristics: pic18fxx39 (industrial, extended) pic18lfxx39 (industrial) pic18lfxx39 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18fxx39 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. symbol characteristic min typ max units conditions v dd supply voltage d001 pic18lfxx39 2.0 ? 5.5 v hs osc mode d001 pic18fxx39 4.2 ? 5.5 v d002 v dr ram data retention voltage (1) 1.5 ? ? v d003 v por v dd start voltage to ensure internal power-on reset signal ? ? 0.7 v see section 3.1 (power-on reset) for details d004 s vdd v dd rise rate to ensure internal power-on reset signal 0.05 ? ? v/ms see section 3.1 (power-on reset) for details v bor brown-out reset voltage d005 pic18lfxx39 borv1:borv0 = 11 1.98 ? 2.14 v 85 c t 25 c borv1:borv0 = 10 2.67 ? 2.89 v borv1:borv0 = 01 4.16 ? 4.5 v borv1:borv0 = 00 4.45 ? 4.83 v d005 pic18fxx39 borv1:borv0 = 1x n.a. ? n.a. v not in operating voltage range of device borv1:borv0 = 01 4.16 ? 4.5 v borv1:borv0 = 00 4.45 ? 4.83 v legend: shading of rows is to assist in readability of the table. note 1: this is the limit to which v dd can be lowered in sleep mode, or during a device reset, without losing ram data. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd mclr = v dd ; wdt enabled/disabled as specified. 3: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in hi-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 4: the lvd and bor modules share a large portion of circuitry. the ? i bor and ? i lvd currents are not additive. once one of these modules is enabled, the other may also be enabled without further penalty.
pic18fxx39 ds30485a-page 262 preliminary ? 2002 microchip technology inc. i dd supply current (2) d010c pic18lfxx39 ?1025ma ec, ecio osc configurations v dd = 4.2v, -40 c to +85 c d010c pic18fxx39 ? 10 25 ma ec, ecio osc configurations v dd = 4.2v, -40 c to +125 c d013 pic18lfxx39 ? ? 10 15 15 25 ma ma hs osc configuration f osc = 25 mhz, v dd = 5.5v hs + pll osc configurations f osc = 10 mhz, v dd = 5.5v d013 pic18fxx39 ? ? 10 15 15 25 ma ma hs osc configuration f osc = 25 mhz, v dd = 5.5v hs + pll osc configurations f osc = 10 mhz, v dd = 5.5v i pd power-down current (3) d020 pic18lfxx39 ? ? ? 0.08 0.1 3 0.9 4 10 a a a v dd = 2.0v, +25 c v dd = 2.0v, -40 c to +85 c v dd = 4.2v, -40 c to +85 c d020 d021b pic18fxx39 ? ? ? .1 3 15 .9 10 25 a a a v dd = 4.2v, +25 c v dd = 4.2v, -40 c to +85 c v dd = 4.2v, -40 c to +125 c 23.1 dc characteristics: pic18fxx39 (industrial, extended) pic18lfxx39 (industrial) (continued) pic18lfxx39 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18fxx39 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. symbol characteristic min typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: this is the limit to which v dd can be lowered in sleep mode, or during a device reset, without losing ram data. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd mclr = v dd ; wdt enabled/disabled as specified. 3: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in hi-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 4: the lvd and bor modules share a large portion of circuitry. the ? i bor and ? i lvd currents are not additive. once one of these modules is enabled, the other may also be enabled without further penalty.
? 2002 microchip technology inc. preliminary ds30485a-page 263 pic18fxx39 module differential current d022 ? i wdt watchdog timer pic18lfxx39 ? ? ? 0.75 2 10 1.5 8 25 a a a v dd = 2.0v, +25 c v dd = 2.0v, -40 c to +85 c v dd = 4.2v, -40 c to +85 c d022 watchdog timer pic18fxx39 ? ? ? 7 10 25 15 25 40 a a a v dd = 4.2v, +25 c v dd = 4.2v, -40 c to +85 c v dd = 4.2v, -40 c to +125 c d022a ? i bor brown-out reset (4) pic18lfxx39 ? ? ? 29 29 33 35 45 50 a a a v dd = 2.0v, +25 c v dd = 2.0v, -40 c to +85 c v dd = 4.2v, -40 c to +85 c d022a brown-out reset (4) pic18fxx39 ? ? ? 36 36 36 40 50 65 a a a v dd = 4.2v, +25 c v dd = 4.2v, -40 c to +85 c v dd = 4.2v, -40 c to +125 c d022b ? i lvd low voltage detect (4) pic18lfxx39 ? ? ? 29 29 33 35 45 50 a a a v dd = 2.0v, +25 c v dd = 2.0v, -40 c to +85 c v dd = 4.2v, -40 c to +85 c d022b low voltage detect (4) pic18fxx39 ? ? ? 33 33 33 40 50 65 a a a v dd = 4.2v, +25 c v dd = 4.2v, -40 c to +85 c v dd = 4.2v, -40 c to +125 c 23.1 dc characteristics: pic18fxx39 (industrial, extended) pic18lfxx39 (industrial) (continued) pic18lfxx39 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18fxx39 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. symbol characteristic min typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: this is the limit to which v dd can be lowered in sleep mode, or during a device reset, without losing ram data. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd mclr = v dd ; wdt enabled/disabled as specified. 3: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in hi-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 4: the lvd and bor modules share a large portion of circuitry. the ? i bor and ? i lvd currents are not additive. once one of these modules is enabled, the other may also be enabled without further penalty.
pic18fxx39 ds30485a-page 264 preliminary ? 2002 microchip technology inc. 23.2 dc characteristics: pic18fxx39 (industrial, extended) pic18lfxx39 (industrial) dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. symbol characteristic min max units conditions v il input low voltage i/o ports: d030 with ttl buffer vss 0.15 v dd vv dd < 4.5v d030a ? 0.8 v 4.5v v dd 5.5v d031 with schmitt trigger buffer rc3 and rc4 vss vss 0.2 v dd 0.3 v dd v v d032 mclr v ss 0.2 v dd v d032a osc1 (hs mode) v ss 0.3 v dd v d033 osc1 (ec mode) v ss 0.2 v dd v v ih input high voltage i/o ports: d040 with ttl buffer 0.25 v dd + 0.8v v dd vv dd < 4.5v d040a 2.0 v dd v4.5v v dd 5.5v d041 with schmitt trigger buffer rc3 and rc4 0.8 v dd 0.7 v dd v dd v dd v v d042 mclr , osc1 (ec mode) 0.8 v dd v dd v d042a osc1 (hs mode) 0.7 v dd v dd v i il input leakage current (1,2) d060 i/o ports .02 1 av ss v pin v dd , pin at hi-impedance d061 mclr ? 1 avss v pin v dd d063 osc1 ? 1 avss v pin v dd i pu weak pull-up current d070 i purb portb weak pull-up current 50 450 av dd = 5v, v pin = v ss note 1: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 2: negative current is defined as current sourced by the pin. 3: parameter is characterized but not tested.
? 2002 microchip technology inc. preliminary ds30485a-page 265 pic18fxx39 v ol output low voltage d080 i/o ports ? 0.6 v i ol = 8.5 ma, v dd = 4.5v, -40 c to +85 c d080a ? 0.6 v i ol = 7.0 ma, v dd = 4.5v, -40 c to +125 c v oh output high voltage (2) d090 i/o ports v dd ? 0.7 ? v i oh = -3.0 ma, v dd = 4.5v, -40 c to +85 c d090a v dd ? 0.7 ? v i oh = -2.5 ma, v dd = 4.5v, -40 c to +125 c d150 v od open drain high voltage ? 8.5 v ra4 pin capacitive loading specs on output pins d100 (3) c osc2 osc2 pin ? 15 pf in hs mode when external clock is used to drive osc1 d101 c io all i/o pins ? 50 pf to meet the ac timing specifications d102 c b scl, sda ? 400 pf in i 2 c mode 23.2 dc characteristics: pic18fxx39 (industrial, extended) pic18lfxx39 (industrial) (continued) dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. symbol characteristic min max units conditions note 1: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 2: negative current is defined as current sourced by the pin. 3: parameter is characterized but not tested.
pic18fxx39 ds30485a-page 266 preliminary ? 2002 microchip technology inc. figure 23-3: low voltage detect characteristics table 23-1: low voltage detect characteristics v lvd lvdif v dd (lvdif set by hardware) (lvdif can be cleared in software) 37 standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. symbol characteristic min typ max units conditions d420 v lvd lvd voltage on v dd transition high to low lvv = 0001 1.98 2.06 2.14 v t 25 c lvv = 0010 2.18 2.27 2.36 v t 25 c lvv = 0011 2.37 2.47 2.57 v t 25 c lvv = 0100 2.48 2.58 2.68 v lvv = 0101 2.67 2.78 2.89 v lvv = 0110 2.77 2.89 3.01 v lvv = 0111 2.98 3.1 3.22 v lvv = 1000 3.27 3.41 3.55 v lvv = 1001 3.47 3.61 3.75 v lvv = 1010 3.57 3.72 3.87 v lvv = 1011 3.76 3.92 4.08 v lvv = 1100 3.96 4.13 4.3 v lvv = 1101 4.16 4.33 4.5 v lvv = 1110 4.45 4.64 4.83 v
? 2002 microchip technology inc. preliminary ds30485a-page 267 pic18fxx39 table 23-2: memory programming requirements dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended param no. sym characteristic min typ? max units conditions internal program memory programming specifications d110 v pp voltage on mclr /v pp pin 9.00 ? 13.25 v d113 i ddp supply current during programming ??10ma data eeprom memory d120 e d cell endurance 100k 1m ? e/w -40 c to +85 c d121 v drw v dd for read/write v min ? 5.5 v using eecon to read/write v min = minimum operating voltage d122 t dew erase/write cycle time ? 4 ? ms d123 t retd characteristic retention 40 ? ? year provided no other specifications are violated d123a t retd characteristic retention 100 ? ? year 25 c (note 1) d124 t ref number of total erase/write cycles before refresh (2) 1m 10m ? e/w -40c to +85c program flash memory d130 e p cell endurance 10k 100k ? e/w -40 c to +85 c d131 v pr v dd for read v min ? 5.5 v v min = minimum operating voltage d132 v ie v dd for block erase 4.5 ? 5.5 v using icsp port d132a v iw v dd for externally timed erase or write 4.5 ? 5.5 v using icsp port d132b v pew v dd for self-timed write v min ? 5.5 v v min = minimum operating voltage d133 t ie icsp block erase cycle time ? 4 ? ms v dd 4.5v d133a t iw icsp erase or write cycle time (externally timed) 1 ?? ms v dd 4.5v d133a t iw self-timed write cycle time ? 2 ? ms d134 t retd characteristic retention 40 ? ? year provided no other specifications are violated d134a t retd characteristic retention 100 ? ? year 25 c (note 1) ? data in ?typ? column is at 5.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: retention time is valid, provided no other specifications are violated. 2: refer to section 6.8 for a more detailed discussion on data eeprom endurance.
pic18fxx39 ds30485a-page 268 preliminary ? 2002 microchip technology inc. 23.3 ac (timing) characteristics 23.3.1 timing parameter symbology the timing parameter symbols have been created following one of the following formats: 1. tpps2pps 3. t cc : st (i 2 c specifications only) 2. tpps 4. ts (i 2 c specifications only) t f frequency t time lowercase letters (pp) and their meanings: pp cc ccp1 osc osc1 ck clko rd rd cs cs rw rd or wr di sdi sc sck do sdo ss ss dt data in t0 t0cki io i/o port t1 t13cki mc mclr wr wr uppercase letters and their meanings: s f fall p period hhigh rrise i invalid (hi-impedance) v valid l low z hi-impedance i 2 c only aa output access high high buf bus free low low t cc : st (i 2 c specifications only) cc hd hold su setup st dat data input hold sto stop condition sta start condition
? 2002 microchip technology inc. preliminary ds30485a-page 269 pic18fxx39 23.3.2 timing conditions the temperature and voltages specified in table 23-3 apply to all timing specifications unless otherwise noted. figure 23-4 specifies the load conditions for the timing specifications. table 23-3: temperature and voltage specifications - ac figure 23-4: load conditions for device timing specifications ac characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial -40c t a +125c for extended operating voltage v dd range as described in dc spec section 23.1 and section 23.2. lc parts operate for industrial temperatures only. v dd /2 c l r l pin pin v ss v ss c l r l =464 ? c l = 50 pf for all pins except osc2/clko and including d and e outputs as ports load condition 1 load condition 2
pic18fxx39 ds30485a-page 270 preliminary ? 2002 microchip technology inc. 23.3.3 timing diagrams and specifications figure 23-5: external clock timing (all modes except pll) table 23-4: external clock timing requirements table 23-5: pll clock timing specifications (v dd = 4.2 to 5.5v) osc1 clko q4 q1 q2 q3 q4 q1 1 2 3 3 4 4 param. no. symbol characteristic min max units conditions 1a f osc external clki frequency (1) oscillator frequency (1) dc 40 mhz ec, ecio, -40 c to +85 c dc 25 mhz ec, ecio, +85 c to +125 c 4 25 mhz hs osc 4 10 mhz hs + pll osc, -40 c to +85 c 4 6.25 mhz hs + pll osc, +85 c to +125 c 1 t osc external clki period (1) oscillator period (1) 25 ? ns ec, ecio, -40 c to +85 c 40 ? ns ec, ecio, +85 c to +125 c 40 250 ns hs osc 100 250 ns hs + pll osc, -40 c to +85 c 160 250 ns hs + pll osc, +85 c to +125 c 2 t cy instruction cycle time (1) 100 ? ns t cy = 4/f osc , -40 c to +85 c 160 ? ns t cy = 4/f osc , +85 c to +125 c 3 tosl, to s h external clock in (osc1) high or low time 10 ? ns hs osc 4tosr, to s f external clock in (osc1) rise or fall time ?7.5nshs osc note 1: instruction cycle period (t cy ) equals four times the input oscillator time-base period for all configurations except pll. all specified values are based on characterization data for that particular oscillator type under standard operating conditions with the device executing code. exceeding these specified limits may result in an unstable oscillator operation and/or higher than expected current consumption. all devices are tested to operate at ?min.? values with an external clock applied to the osc1/clki pin. when an external clock input is used, the ?max.? cycle time limit is ?dc? (no clock) for all devices. param no. sym characteristic min typ? max units conditions ?f osc oscillator frequency range 4 ? 10 mhz hs mode only ?f sys on-chip vco system frequency 16 ? 40 mhz hs mode only ?t rc pll start-up time (lock time) ? ? 2 ms ? ? clk clko stability (jitter) -2 ? +2 % ? data in ?typ? column is at 5v, 25 c unless otherwise stated. these parameters are for design guidance only and are not tested.
? 2002 microchip technology inc. preliminary ds30485a-page 271 pic18fxx39 figure 23-6: clko and i/o timing table 23-6: clko and i/o timing requirements param. no. symbol characteristic min typ max units conditions 10 tosh2ckl osc1 to clko ?75200ns (note 1) 11 tosh2ckh osc1 to clko ?75200ns (note 1) 12 tckr clko rise time ? 35 100 ns (note 1) 13 tckf clko fall time ? 35 100 ns (note 1) 14 tckl2iov clko to port out valid ? ? 0.5 t cy + 20 ns (note 1) 15 tiov2ckh port in valid before clko 0.25 t cy + 25 ? ? ns (note 1) 16 tckh2ioi port in hold after clko 0??ns (note 1) 17 tosh2iov osc1 (q1 cycle) to port out valid ? 50 150 ns 18 tosh2ioi osc1 (q2 cycle) to port input invalid (i/o in hold time) pic18 f xxxx 100 ? ? ns 18a pic18 lf xxxx 200 ? ? ns 19 tiov2osh port input valid to osc1 (i/o in setup time) 0 ? ? ns 20 tior port output rise time pic18 f xxxx ?1025ns 20a pic18 lf xxxx ? ? 60 ns v dd = 2v 21 tiof port output fall time pic18 f xxxx ?1025ns 21a pic18 lf xxxx ? ? 60 ns v dd = 2v 22?? t inp int pin high or low time t cy ??ns 23?? t rbp rb7:rb4 change int high or low time t cy ??ns 24?? t rcp rc7:rc4 change int high or low time 20 ns ?? these parameters are asynchronous events not related to any internal clock edges. note 1: measurements are taken in rc mode, where clko output is 4 x t osc . note: refer to figure 23-4 for load conditions. osc1 clko i/o pin (input) i/o pin (output) q4 q1 q2 q3 10 13 14 17 20, 21 19 18 15 11 12 16 old value new value
pic18fxx39 ds30485a-page 272 preliminary ? 2002 microchip technology inc. figure 23-7: reset, watchdog timer, oscillator start-up timer and power-up timer timing figure 23-8: brown-out reset timing table 23-7: reset, watchdog timer, oscillator start-up timer, power-up timer and brown-out reset requirements param. no. symbol characteristic min typ max units conditions 30 tmcl mclr pulse width (low) 2 ? ? s 31 t wdt watchdog timer time-out period (no postscaler) 71833ms 32 t ost oscillation start-up timer period 1024 t osc ? 1024 t osc ?t osc = osc1 period 33 t pwrt power up timer period 28 72 132 ms 34 tioz i/o high impedance from mclr low or watchdog timer reset ?2? s 35 t bor brown-out reset pulse width 200 ? ? sv dd b vdd (see d005) 36 t ivrst time for internal reference voltage to become stable ?20500 s 37 t lvd low voltage detect pulse width 200 ? ? sv dd v lvd (see d420) v dd mclr internal por pwrt time-out osc time-out internal reset watchdog timer reset 33 32 30 31 34 i/o pins 34 note: refer to figure 23-4 for load conditions. v dd bv dd 35 v bgap = 1.2v v irvst enable internal reference voltage internal reference voltage stable 36 typical
? 2002 microchip technology inc. preliminary ds30485a-page 273 pic18fxx39 figure 23-9: timer0 and timer1 external clock timings table 23-8: timer0 and timer1 external clock requirements note: refer to figure 23-4 for load conditions. 46 47 45 48 41 42 40 t0cki t13cki tmr0 or tmr1 param no. symbol characteristic min max units conditions 40 tt0h t0cki high pulse width no prescaler 0.5t cy + 20 ? ns with prescaler 10 ? ns 41 tt0l t0cki low pulse width no prescaler 0.5t cy + 20 ? ns with prescaler 10 ? ns 42 tt0p t0cki period no prescaler t cy + 10 ? ns with prescaler greater of: 20 n s or t cy + 40 n ? ns n = prescale value (1, 2, 4,..., 256) 45 tt1h t13cki high time synchronous, no prescaler 0.5t cy + 20 ? ns synchronous, with prescaler pic18 f xxxx 10 ? ns pic18 lf xxxx 25 ? ns asynchronous pic18 f xxxx 30 ? ns pic18 lf xxxx 50 ? ns 46 tt1l t13cki low time synchronous, no prescaler 0.5t cy + 5 ? ns synchronous, with prescaler pic18 f xxxx 10 ? ns pic18 lf xxxx 25 ? ns asynchronous pic18 f xxxx 30 ? ns pic18 lf xxxx 50 ? ns 47 tt1p t13cki input period synchronous greater of: 20 n s or t cy + 40 n ? ns n = prescale value (1, 2, 4, 8) asynchronous 60 ? ns ft1 t13cki oscillator input frequency range dc 50 khz 48 tcke2tmri delay from external t13cki clock edge to timer increment 2 t osc 7 t osc ?
pic18fxx39 ds30485a-page 274 preliminary ? 2002 microchip technology inc. figure 23-10: pwm timings (pwm1 and pwm2) table 23-9: pwm timing requirements (pwm1 and pwm2) note: refer to figure 23-4 for load conditions. pwmx output 53 54 param. no. symbol characteristic min max units conditions 53 tccr pwmx output rise time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 54 tccf pwmx output fall time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v
? 2002 microchip technology inc. preliminary ds30485a-page 275 pic18fxx39 figure 23-11: parallel slave port timing (pic18f4x39) table 23-10: parallel slave port requirements (pic18f4x39) note: refer to figure 23-4 for load conditions. re2/cs re0/rd re1/wr rd7:rd0 62 63 64 65 param. no. symbol characteristic min max units conditions 62 tdtv2wrh data in valid before wr or cs (setup time) 20 25 ? ? ns ns extended temp. range 63 twrh2dti wr or cs to data?in invalid (hold time) pic18 f xxxx 20 ? ns pic18 lf xxxx 35 ? ns v dd = 2v 64 trdl2dtv rd and cs to data?out valid ? ? 80 90 ns ns extended temp. range 65 trdh2dti rd or cs to data?out invalid 10 30 ns 66 tibfinh inhibit of the ibf flag bit being cleared from wr or cs ?3 t cy
pic18fxx39 ds30485a-page 276 preliminary ? 2002 microchip technology inc. figure 23-12: example spi master mode timing (cke = 0) table 23-11: example spi mode requirements (master mode, cke = 0) param. no. symbol characteristic min max units conditions 70 tssl2sch, tssl2scl ss to sck or sck input t cy ?ns 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 100 ? ns 73a t b 2 b last clock edge of byte 1 to the 1st clock edge of byte 2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 76 tdof sdo data output fall time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 78 tscr sck output rise time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 79 tscf sck output fall time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18 f xxxx ? 50 ns pic18 lf xxxx ? 150 ns v dd = 2v note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used. ss sck (ckp = 0) sck (ckp = 1) sdo sdi 70 71 72 73 74 75, 76 78 79 80 79 78 msb lsb bit6 - - - - - -1 msb in lsb in bit6 - - - -1 note: refer to figure 23-4 for load conditions.
? 2002 microchip technology inc. preliminary ds30485a-page 277 pic18fxx39 figure 23-13: example spi master mode timing (cke = 1) table 23-12: example spi mode requirements (master mode, cke = 1) param. no. symbol characteristic min max units conditions 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 100 ? ns 73a t b 2 b last clock edge of byte 1 to the 1st clock edge of byte 2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 76 tdof sdo data output fall time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 78 tscr sck output rise time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 79 tscf sck output fall time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18 f xxxx ? 50 ns pic18 lf xxxx ? 150 ns v dd = 2v 81 tdov2sch, tdov2scl sdo data output setup to sck edge t cy ?ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used. ss sck (ckp = 0) sck (ckp = 1) sdo sdi 81 71 72 74 75, 76 78 80 msb 79 73 msb in bit6 - - - - - -1 lsb in bit6 - - - -1 lsb note: refer to figure 23-4 for load conditions.
pic18fxx39 ds30485a-page 278 preliminary ? 2002 microchip technology inc. figure 23-14: example spi slave mode timing (cke = 0) table 23-13: example spi mode requirements (slave mode timing (cke = 0)) param. no. symbol characteristic min max units conditions 70 tssl2sch, ts s l 2 s c l ss to sck or sck input t cy ?ns 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 100 ? ns 73a t b 2 b last clock edge of byte 1 to the first clock edge of byte 2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 76 tdof sdo data output fall time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 77 tssh2doz ss to sdo output hi-impedance 10 50 ns 78 tscr sck output rise time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 79 tscf sck output fall time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18 f xxxx ? 50 ns pic18 lf xxxx ? 150 ns v dd = 2v 83 tsch2ssh, ts c l 2 s s h ss after sck edge 1.5 t cy + 40 ? ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used. ss sck (ckp = 0) sck (ckp = 1) sdo sdi 70 71 72 73 74 75, 76 77 78 79 80 79 78 sdi msb lsb bit6 - - - - - -1 msb in bit6 - - - -1 lsb in 83 note: refer to figure 23-4 for load conditions.
? 2002 microchip technology inc. preliminary ds30485a-page 279 pic18fxx39 figure 23-15: example spi slave mode timing (cke = 1) table 23-14: example spi slave mode requirements (cke = 1) param. no. symbol characteristic min max units conditions 70 tssl2sch, ts s l 2 s c l ss to sck or sck input t cy ?ns 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73a t b 2 b last clock edge of byte 1 to the first clock edge of byte 2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 76 tdof sdo data output fall time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 77 tssh2doz ss to sdo output hi-impedance 10 50 ns 78 tscr sck output rise time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 79 tscf sck output fall time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18 f xxxx ? 50 ns pic18 lf xxxx ? 150 ns v dd = 2v 82 tssl2dov sdo data output valid after ss edge pic18 f xxxx ? 50 ns pic18 lf xxxx ? 150 ns v dd = 2v 83 tsch2ssh, ts c l 2 s s h ss after sck edge 1.5 t cy + 40 ? ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used. ss sck (ckp = 0) sck (ckp = 1) sdo sdi 70 71 72 82 74 75, 76 msb bit6 - - - - - -1 lsb 77 msb in bit6 - - - -1 lsb in 80 83 note: refer to figure 23-4 for load conditions.
pic18fxx39 ds30485a-page 280 preliminary ? 2002 microchip technology inc. figure 23-16: i 2 c bus start/stop bits timing table 23-15: i 2 c bus start/stop bits requirements (slave mode) figure 23-17: i 2 c bus data timing note: refer to figure 23-4 for load conditions. 91 92 93 scl sda start condition stop condition 90 param. no. symbol characteristic min max units conditions 90 t su : sta start condition 100 khz mode 4700 ? ns only relevant for repeated start condition setup time 400 khz mode 600 ? 91 t hd : sta start condition 100 khz mode 4000 ? ns after this period, the first clock pulse is generated hold time 400 khz mode 600 ? 92 t su : sto stop condition 100 khz mode 4700 ? ns setup time 400 khz mode 600 ? 93 t hd : sto stop condition 100 khz mode 4000 ? ns hold time 400 khz mode 600 ? note: refer to figure 23-4 for load conditions. 90 91 92 100 101 103 106 107 109 109 110 102 scl sda in sda out
? 2002 microchip technology inc. preliminary ds30485a-page 281 pic18fxx39 table 23-16: i 2 c bus data requirements (slave mode) param. no. symbol characteristic min max units conditions 100 t high clock high time 100 khz mode 4.0 ? s pic18fxxx must operate at a minimum of 1.5 mhz 400 khz mode 0.6 ? s pic18fxxx must operate at a minimum of 10 mhz ssp module 1.5 t cy ? 101 t low clock low time 100 khz mode 4.7 ? s pic18fxxx must operate at a minimum of 1.5 mhz 400 khz mode 1.3 ? s pic18fxxx must operate at a minimum of 10 mhz ssp module 1.5 t cy ? 102 t r sda and scl rise time 100 khz mode ? 1000 ns 400 khz mode 20 + 0.1 c b 300 ns c b is specified to be from 10 to 400 pf 103 t f sda and scl fall time 100 khz mode ? 1000 ns v dd 4.2v 400 khz mode 20 + 0.1 c b 300 ns v dd 4.2v 90 t su : sta start condition setup time 100 khz mode 4.7 ? s only relevant for repeated start condition 400 khz mode 0.6 ? s 91 t hd : sta start condition hold time 100 khz mode 4.0 ? s after this period, the first clock pulse is generated 400 khz mode 0.6 ? s 106 t hd : dat data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 s 107 t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns 92 t su : sto stop condition setup time 100 khz mode 4.7 ? s 400 khz mode 0.6 ? s 109 t aa output valid from clock 100 khz mode ? 3500 ns (note 1) 400 khz mode ? ? ns 110 t buf bus free time 100 khz mode 4.7 ? s time the bus must be free before a new transmission can start 400 khz mode 1.3 ? s d102 c b bus capacitive loading ? 400 pf note 1: as a transmitter, the device must provide this internal minimum delay time to bridge the undefined region (min. 300 ns) of the falling edge of scl to avoid unintended generation of start or stop conditions. 2: a fast mode i 2 c bus device can be used in a standard mode i 2 c bus system, but the requirement t su : dat 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line. t r max. + t su : dat = 1000 + 250 = 1250 ns (according to the standard mode i 2 c bus specification) before the scl line is released.
pic18fxx39 ds30485a-page 282 preliminary ? 2002 microchip technology inc. figure 23-18: master ssp i 2 c bus start/stop bits timing waveforms table 23-17: master ssp i 2 c bus start/stop bits requirements figure 23-19: master ssp i 2 c bus data timing note: refer to figure 23-4 for load conditions. 91 93 scl sda start condition stop condition 90 92 param. no. symbol characteristic min max units conditions 90 t su : sta start condition 100 khz mode 2(t osc )(brg + 1) ? ns only relevant for repeated start condition setup time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 91 t hd : sta start condition 100 khz mode 2(t osc )(brg + 1) ? ns after this period, the first clock pulse is generated hold time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 92 t su : sto stop condition 100 khz mode 2(t osc )(brg + 1) ? ns setup time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 93 t hd : sto stop condition 100 khz mode 2(t osc )(brg + 1) ? ns hold time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? note 1: maximum pin capacitance = 10 pf for all i 2 c pins. note: refer to figure 23-4 for load conditions. 90 91 92 100 101 103 106 107 109 109 110 102 scl sda in sda out
? 2002 microchip technology inc. preliminary ds30485a-page 283 pic18fxx39 table 23-18: master ssp i 2 c bus data requirements param. no. symbol characteristic min max units conditions 100 t high clock high time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 101 t low clock low time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 102 t r sda and scl rise time 100 khz mode ? 1000 ns c b is specified to be from 10 to 400 pf 400 khz mode 20 + 0.1 c b 300 ns 1 mhz mode (1) ? 300 ns 103 t f sda and scl fall time 100 khz mode ? 1000 ns v dd 4.2v 400 khz mode 20 + 0.1 c b 300 ns v dd 4.2v 90 t su : sta start condition setup time 100 khz mode 2(t osc )(brg + 1) ? ms only relevant for repeated start condition 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 91 t hd : sta start condition hold time 100 khz mode 2(t osc )(brg + 1) ? ms after this period, the first clock pulse is generated 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 106 t hd : dat data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 ms 107 t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns 92 t su : sto stop condition setup time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 109 t aa output valid from clock 100 khz mode ? 3500 ns 400 khz mode ? 1000 ns 1 mhz mode (1) ??ns 110 t buf bus free time 100 khz mode 4.7 ? ms time the bus must be free before a new transmission can start 400 khz mode 1.3 ? ms d102 c b bus capacitive loading ? 400 pf note 1: maximum pin capacitance = 10 pf for all i 2 c pins. 2: a fast mode i 2 c bus device can be used in a standard mode i 2 c bus system, but parameter #107 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line, parameter #102 + parameter #107 = 1000 + 250 = 1250 ns (for 100 khz mode) before the scl line is released.
pic18fxx39 ds30485a-page 284 preliminary ? 2002 microchip technology inc. figure 23-20: usart synchronous transmission (master/slave) timing table 23-19: usart synchronous transmission requirements figure 23-21: usart synchronous receive (master/slave) timing table 23-20: usart synchronous receive requirements 121 121 120 122 rc6/tx/ck rc7/rx/dt pin pin note: refer to figure 23-4 for load conditions. param. no. symbol characteristic min max units conditions 120 tckh2dtv sync xmit (master & slave) clock high to data out valid pic18 f xxxx ? 50 ns pic18 lf xxxx ? 150 ns v dd = 2v 121 tckr clock out rise time and fall time (master mode) pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 122 tdtr data out rise time and fall time pic18 f xxxx ? 25 ns pic18 lf xxxx ? 60 ns v dd = 2v 125 126 rc6/tx/ck rc7/rx/dt pin pin note: refer to figure 23-4 for load conditions. param. no. symbol characteristic min max units conditions 125 tdtv2ckl sync rcv (master & slave) data hold before ck (dt hold time) 10 ? ns 126 tckl2dtl data hold after ck (dt hold time) pic18 f xxxx 15 ? ns pic18 lf xxxx 20 ? ns v dd = 2v
? 2002 microchip technology inc. preliminary ds30485a-page 285 pic18fxx39 table 23-21: a/d converter characteristics: pic18fxx39 (industrial, extended) pic18lfxx39 (industrial) figure 23-22: a/d conversion timing param no. symbol characteristic min typ max units conditions a01 n r resolution ? ? 10 bit a03 e il integral linearity error ? ? <1 lsb v ref = v dd = 5.0v a04 e dl differential linearity error ? ? <1 lsb v ref = v dd = 5.0v a05 e g gain error ? ? <1 lsb v ref = v dd = 5.0v a06 e off offset error ? ? <1.5 lsb v ref = v dd = 5.0v a10 ? monotonicity guaranteed (2) ?v ss v ain v ref a20 a20a v ref reference voltage (v refh ? v refl ) 1.8v 3v ? ? ? ? v v v dd < 3.0v v dd 3.0v a21 v refh reference voltage high av ss ?av dd + 0.3v v a22 v refl reference voltage low av ss ? 0.3v ? v refh v a25 v ain analog input voltage av ss ? 0.3v ? av dd + 0.3v v v dd 2.5v (note 3) a30 z ain recommended impedance of analog voltage source ?? 2.5k ? (note 4) a50 i ref v ref input current (note 1) ? ? ? ? 5 150 a a during v ain acquisition during a/d conversion cycle note 1: vss v ain v ref 2: the a/d conversion result never decreases with an increase in the input voltage, and has no missing codes. 3: for v dd < 2.5v, v ain should be limited to < .5 v dd . 4: maximum allowed impedance for analog voltage source is 10 k ? . this requires higher acquisition times. 131 130 132 bsf adcon0, go q4 a/d clk a/d data adres adif go sample old_data sampling stopped done new_data (note 2) 98 7 2 1 0 note 1: if the a/d clock source is selected as rc, a time of t cy is added before the a/d clock starts. this allows the sleep instruction to be executed. 2: this is a minimal rc delay (typically 100 ns), which also disconnects the holding capacitor from the analog input. . . . . . . t cy
pic18fxx39 ds30485a-page 286 preliminary ? 2002 microchip technology inc. table 23-22: a/d conversion requirements param no. symbol characteristic min max units conditions 130 t ad a/d clock period pic18 f xxxx 1.6 20 (4) st osc based pic18 lf xxxx 2.0 6.0 s a/d rc mode 131 t cnv conversion time (not including acquisition time) (note 1) 11 12 t ad 132 t acq acquisition time (note 2) 5 10 ? ? s s v ref = v dd = 5.0v v ref = v dd = 2.5v 135 t swc switching time from convert sample ? (note 3) note 1: adres register may be read on the following t cy cycle. 2: the time for the holding capacitor to acquire the ?new? input voltage, when the new input value has not changed by more than 1 lsb from the last sampled voltage. the source impedance ( r s ) on the input channels is 50 ? . see section 18.0 for more information on acquisition time consideration. 3: on the next q4 cycle of the device clock. 4: the time of the a/d clock period is dependent on the device frequency and the t ad clock divider.
? 2002 microchip technology inc. preliminary ds30485a-page 287 pic18fxx39 24.0 dc and ac characteristics graphs and tables ?typical? represents the mean of the distribution at 25 c. ?maximum? or ?minimum? represents (mean + 3 ) or (mean - 3 ) respectively, where is a standard deviation, over the whole temperature range. figure 24-1: typical i dd vs. f osc over v dd (hs mode) figure 24-2: maximum i dd vs. f osc over v dd (hs mode) note: the graphs and tables provided following this note are a statistical summary based on a limited number of samples and are provided for informational purposes only. the performance characteristics listed herein are not tested or guaranteed. in some graphs or tables, the data presented may be outside the specified operating range (e.g., outside specified power supply range) and therefore, outside the warranted range. 0 2 4 6 8 10 12 4 6 8 101214161820222426 f osc (mhz) i dd (ma) 5.5v 5.0v 4.5v 4.0v 3.5v 3.0v 2.5v 2.0v typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) 0 2 4 6 8 10 12 4 6 8 101214161820222426 f osc (mhz) i dd (ma) 5.5v 5.0v 4.5v 4.0v 3.5v 3.0v 2.5v 2.0v typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c)
pic18fxx39 ds30485a-page 288 preliminary ? 2002 microchip technology inc. figure 24-3: typical i dd vs. f osc over v dd (hs/pll mode) figure 24-4: maximum i dd vs. f osc over v dd (hs/pll mode) 0 2 4 6 8 10 12 14 16 18 20 45678910 f osc (mhz) i dd (ma) 5.5v 5.0v 4.5v 4.2v typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) 0 2 4 6 8 10 12 14 16 18 20 45678910 f osc (mhz) i dd (ma) 5.5v 5.0v 4.5v 4.2v typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c)
? 2002 microchip technology inc. preliminary ds30485a-page 289 pic18fxx39 figure 24-5: typical i dd vs. f osc over v dd (ec mode) figure 24-6: maximum i dd vs. f osc over v dd (ec mode) 0 2 4 6 8 10 12 14 16 4 8 12 16 20 24 28 32 36 40 f osc (mhz) i dd (ma) 5.5v 5.0v 4.5v 4.0v 3.5v 3.0v 2.5v 2.0v 4.2v typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) 0 2 4 6 8 10 12 14 16 4 8 12 16 20 24 28 32 36 40 f osc (mhz) i dd (ma) 5.5v 5.0v 4.5v 4.0v 3.5v 3.0v 2.5v 2.0v 4.2v typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c)
pic18fxx39 ds30485a-page 290 preliminary ? 2002 microchip technology inc. figure 24-7: i pd vs. v dd , -40 c to +125 c (sleep mode, all peripherals disabled) figure 24-8: ? i bor vs. v dd over temperature (bor enabled, v bor = 2.00 - 2.16v) 0.01 0.1 1 10 100 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 v dd (v) i pd (ua) typ (+25c) max (+85c) max (-40c to +125c) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) 0 10 20 30 40 50 60 70 80 90 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 v dd (v) i dd ( a) max (125c) max (85c) typ (25c) device held in reset device in sleep max (+125c) max (+85c) typ (+25c) device held in reset device in sleep
? 2002 microchip technology inc. preliminary ds30485a-page 291 pic18fxx39 figure 24-9: typical and maximum ? i wdt vs. v dd over temperature (wdt enabled) figure 24-10: typical, minimum and maximum wdt period vs. v dd (-40 c to +125 c) 0 10 20 30 40 50 60 70 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 v dd (v) i pd ( a) max (125c) max (85c) typ (25c) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) max (+125c) max (+85c) typ (+25c) 0 5 10 15 20 25 30 35 40 45 50 2.02.53.03.54.04.55.05.5 v dd (v) wdt period (ms) max (125c) max (85c) typ (25c) min (-40c) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) max (+125c) max (+85c) typ (+25c) min (-40c)
pic18fxx39 ds30485a-page 292 preliminary ? 2002 microchip technology inc. figure 24-11: ? i lvd vs. v dd over temperature (lvd enabled, v lvd = 4.5 - 4.78v) figure 24-12: typical, minimum and maximum v oh vs. i oh (v dd = 5v, -40 c to +125 c) 0 10 20 30 40 50 60 70 80 90 2.02.53.03.54.04.55.05.5 v dd (v) i dd ( a) max (125c) typ (25c) max (125c) typ (25c) lvdif is set b y hardware lvdif can be cleared by firmware lvdif state is unknown max (+125c) max (+125c) typ (+25c) typ (+25c) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 0 5 10 15 20 25 i oh (-ma) v oh (v) typ (25c) max min max typ (+25c) min
? 2002 microchip technology inc. preliminary ds30485a-page 293 pic18fxx39 figure 24-13: typical, minimum and maximum v oh vs. i oh (v dd = 3v, -40 c to +125 c) figure 24-14: typical and maximum v ol vs. i ol (v dd = 5v, -40 c to +125 c) 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 5 10 15 20 25 i oh (-ma) v oh (v) typ (25c) max min typ (+25c) min max 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 0 5 10 15 20 25 i ol (-ma) v ol (v) max typ (25c) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) typ (+25c) max
pic18fxx39 ds30485a-page 294 preliminary ? 2002 microchip technology inc. figure 24-15: typical and maximum v ol vs. i ol (v dd = 3v, -40 c to +125 c) figure 24-16: minimum and maximum v in vs. v dd (st input, -40 c to +125 c) 0.0 0.5 1.0 1.5 2.0 2.5 0 5 10 15 20 25 i ol (-ma) v ol (v) max typ (25c) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) typ (+25c) max 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 v dd (v) v in (v) v ih max v ih min v il max v il min typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c)
? 2002 microchip technology inc. preliminary ds30485a-page 295 pic18fxx39 figure 24-17: minimum and maximum v in vs. v dd (ttl input, -40 c to +125 c) figure 24-18: minimum and maximum v in vs. v dd (i 2 c input, -40 c to +125 c) 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 v dd (v) v in (v) v th (max) v th (min) typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c) 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 v dd (v) v in (v) v ih max v ih min v il max v il min typical: statistical mean @ 25c maximum: mean + 3 (-40c to 125c) minimum: mean ? 3 (-40c to 125c)
pic18fxx39 ds30485a-page 296 preliminary ? 2002 microchip technology inc. figure 24-19: a/d non-linearity vs. v refh (v dd = v refh , -40 c to +125 c) figure 24-20: a/d non-linearity vs. v refh (v dd = 5v, -40 c to +125 c) 0 0.5 1 1.5 2 2.5 3 3.5 4 22.533.544.555.5 v dd and v refh (v) differential or integral nonlinearity (lsb) -40c 25c 85c 125c -40c +25c +85c +125c 0 0.5 1 1.5 2 2.5 3 22.533.544.555.5 v refh (v) differential or integral nonlinearilty (lsb) max (-40c to 125c) typ (25c) typ (+25c) max (-40c to +125c)
? 2002 microchip technology inc. preliminary ds30485a-page 297 pic18fxx39 25.0 packaging information 25.1 package marking information 28-lead soic yywwnnn example 28-lead pdip (skinny dip) example xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx legend: xx...x customer specific information* y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week ?01?) nnn alphanumeric traceability code note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line thus limiting the number of available characters for customer specific information. * standard picmicro device marking consists of microchip part number, year code, week code, and traceability code. for picmicro device marking beyond this, certain price adders apply. please check with your microchip sales office. for qtp devices, any special marking adders are included in qtp price. 0217017 pic18f2439-i/sp 0210017 pic18f2439-e/so xxxxxxxxxxxxxxxxx yywwnnn xxxxxxxxxxxxxxxxx
pic18fxx39 ds30485a-page 298 preliminary ? 2002 microchip technology inc. package marking information (cont?d) 44-lead tqfp example 44-lead qfn example xxxxxxxxxxxxxxxxxx yywwnnn 40-lead pdip example xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx pic18f4439-i/p 0212017 xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx yywwnnn pic18F4539 -e/pt 0220017 xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx yywwnnn pic18f4439 -i/ml 0220017
? 2002 microchip technology inc. preliminary ds30485a-page 299 pic18fxx39 25.2 package details the following sections give the technical details of the packages. 28-lead skinny plastic dual in-line (sp) ? 300 mil (pdip) 15 10 5 15 10 5 mold draft angle bottom 15 10 5 15 10 5 mold draft angle top 10.92 8.89 8.13 .430 .350 .320 eb overall row spacing 0.56 0.48 0.41 .022 .019 .016 b lower lead width 1.65 1.33 1.02 .065 .053 .040 b1 upper lead width 0.38 0.29 0.20 .015 .012 .008 c lead thickness 3.43 3.30 3.18 .135 .130 .125 l tip to seating plane 35.18 34.67 34.16 1.385 1.365 1.345 d overall length 7.49 7.24 6.99 .295 .285 .275 e1 molded package width 8.26 7.87 7.62 .325 .310 .300 e shoulder to shoulder width 0.38 .015 a1 base to seating plane 3.43 3.30 3.18 .135 .130 .125 a2 molded package thickness 4.06 3.81 3.56 .160 .150 .140 a top to seating plane 2.54 .100 p pitch 28 28 n number of pins max nom min max nom min dimension limits millimeters inches* units 2 1 d n e1 c eb e p l a2 b b1 a a1 notes: jedec equivalent: mo-095 drawing no. c04-070 * controlling parameter dimension d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. significant characteristic
pic18fxx39 ds30485a-page 300 preliminary ? 2002 microchip technology inc. 28-lead plastic small outline (so) ? wide, 300 mil (soic) foot angle top 048048 15 12 0 15 12 0 mold draft angle bottom 15 12 0 15 12 0 mold draft angle top 0.51 0.42 0.36 .020 .017 .014 b lead width 0.33 0.28 0.23 .013 .011 .009 c lead thickness 1.27 0.84 0.41 .050 .033 .016 l foot length 0.74 0.50 0.25 .029 .020 .010 h chamfer distance 18.08 17.87 17.65 .712 .704 .695 d overall length 7.59 7.49 7.32 .299 .295 .288 e1 molded package width 10.67 10.34 10.01 .420 .407 .394 e overall width 0.30 0.20 0.10 .012 .008 .004 a1 standoff 2.39 2.31 2.24 .094 .091 .088 a2 molded package thickness 2.64 2.50 2.36 .104 .099 .093 a overall height 1.27 .050 p pitch 28 28 n number of pins max nom min max nom min dimension limits millimeters inches* units 2 1 d p n b e e1 l c 45 h a2 a a1 * controlling parameter notes: dimensions d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. jedec equivalent: ms-013 drawing no. c04-052 significant characteristic
? 2002 microchip technology inc. preliminary ds30485a-page 301 pic18fxx39 40-lead plastic dual in-line (p) ? 600 mil (pdip) 15 10 5 15 10 5 mold draft angle bottom 15 10 5 15 10 5 mold draft angle top 17.27 16.51 15.75 .680 .650 .620 eb overall row spacing 0.56 0.46 0.36 .022 .018 .014 b lower lead width 1.78 1.27 0.76 .070 .050 .030 b1 upper lead width 0.38 0.29 0.20 .015 .012 .008 c lead thickness 3.43 3.30 3.05 .135 .130 .120 l tip to seating plane 52.45 52.26 51.94 2.065 2.058 2.045 d overall length 14.22 13.84 13.46 .560 .545 .530 e1 molded package width 15.88 15.24 15.11 .625 .600 .595 e shoulder to shoulder width 0.38 .015 a1 base to seating plane 4.06 3.81 3.56 .160 .150 .140 a2 molded package thickness 4.83 4.45 4.06 .190 .175 .160 a top to seating plane 2.54 .100 p pitch 40 40 n number of pins max nom min max nom min dimension limits millimeters inches* units a2 1 2 d n e1 c eb e p l b b1 a a1 * controlling parameter notes: dimensions d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. jedec equivalent: mo-011 drawing no. c04-016 significant characteristic
pic18fxx39 ds30485a-page 302 preliminary ? 2002 microchip technology inc. 44-lead plastic thin quad flatpack (pt) 10x10x1 mm body, 1.0/0.10 mm lead form (tqfp) * controlling parameter notes: dimensions d1 and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. jedec equivalent: ms-026 drawing no. c04-076 1.14 0.89 0.64 .045 .035 .025 ch pin 1 corner chamfer 1.00 .039 (f) footprint (reference) (f) a a1 a2 e e1 #leads=n1 p b d1 d n 1 2 c l units inches millimeters* dimension limits min nom max min nom max number of pins n 44 44 pitch p .031 0.80 overall height a .039 .043 .047 1.00 1.10 1.20 molded package thickness a2 .037 .039 .041 0.95 1.00 1.05 standoff a1 .002 .004 .006 0.05 0.10 0.15 foot length l .018 .024 .030 0.45 0.60 0.75 foot angle 03.5 7 03.5 7 overall width e .463 .472 .482 11.75 12.00 12.25 overall length d .463 .472 .482 11.75 12.00 12.25 molded package width e1 .390 .394 .398 9.90 10.00 10.10 molded package length d1 .390 .394 .398 9.90 10.00 10.10 pins per side n1 11 11 lead thickness c .004 .006 .008 0.09 0.15 0.20 lead width b .012 .015 .017 0.30 0.38 0.44 mold draft angle top 51015 51015 mold draft angle bottom 51015 51015 ch x 45 significant characteristic
? 2002 microchip technology inc. preliminary ds30485a-page 303 pic18fxx39 44-lead plastic quad flat no lead package (ml) 8x8 mm body (qfn) lead width *controlling parameter drawing no. c04-103 notes: dimensions d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010" (0.254mm) per side. b .012 .013 .013 0.30 0.33 0.35 pitch number of pins overall width standoff overall length overall height max units dimension limits a1 d e n p a .315 bsc .000 inches .026 bsc min 44 nom max .002 0 8.00 bsc millimeters* .039 min 44 0.65 bsc nom 0.05 1.00 .010 ref base thickness a3 0.25 ref jedec equivalent: m0-220 0.90 .035 .001 0.02 .315 bsc 8.00 bsc lead length l .014 .016 .018 0.35 0.40 0.45 e2 d2 exposed pad width exposed pad length .262 .268 .274 6.65 6.80 6.95 .262 .268 .274 6.65 6.80 6.95 d2 d a1 a3 a top view n 1 l e2 bottom view b e 2 pad metal exposed p pin 1 index on exposed pad top marking index on optional pin 1 .031 0.80
pic18fxx39 ds30485a-page 304 preliminary ? 2002 microchip technology inc. 44-lead quad flat no lead package (ml) 8x8 mm body (qfn) land pattern and solder mask pad width *controlling parameter drawing no. c04-2103 b ____________ pitch max units dimension limits p inches .026 bsc min nom max millimeters* min 0.65 bsc nom pad length pad to solder mask l ____________ m .005 .006 0.13 0.15 p m m b package edge solder mask
? 2002 microchip technology inc. preliminary ds30485a-page 305 pic18fxx39 appendix a: revision history revision a (november 2002) original data sheet for the pic18fxx39 family. appendix b: device differences the differences between the devices listed in this data sheet are shown in table b-1. table b-1: device differences feature pic18f2439 pic18f2539 pic18f4439 pic18F4539 program memory (kbytes) 12 24 12 24 data memory (bytes) 640 1408 640 1408 a/d channels 5 5 8 8 parallel slave port (psp) no no yes yes package types 28-pin dip 28-pin soic 28-pin dip 28-pin soic 40-pin dip 44-pin tqfp 44-pin qfn 40-pin dip 44-pin tqfp 44-pin qfn
pic18fxx39 ds30485a-page 306 preliminary ? 2002 microchip technology inc. appendix c: conversion considerations the considerations for converting applications from previous versions of pic18 microcontrollers (i.e., pic18fxx2 devices) are listed in table c-1. a specific list of resources that are unavailable to pic18fxx2 applications in pic18fxx39 devices is presented in table c-2. table c-1: conversion considerations between pic18fxx2 and pic18fxx39 devices table c-2: unavailable resources (compared to pic18fxx2) characteristic pic18fxx2 pic18fxx39 pins 28/40/44 28/40/44 available packages dip, pdip, soic, plcc, qfn, tqfp dip, pdip, soic, qfn, tqfp voltage range 2.0 - 5.5v 2.0 - 5.5v frequency range dc - 40 mhz 4 - 40 mhz (20 mhz optimal) available program memory (bytes) 16k or 32k 12k or 24k available data ram (bytes) 768 or 1536 640 or 1408 data eeprom 256 256 interrupt sources 17 or 18 15 or 16 interrupt priority levels two levels: low priority (vector at 0008h) high priority (vector at 0018h) one level when using motor control: vector at 0008h timers (available to users) 4 3 timer1 oscillator option yes no oscillator switching yes no capture/compare/pwm 2 ccp 2 pwm only, available only through motor control kernel motor control kernel no yes a/d 10-bit, 5 or 8 channels, 7 conversion clock selects 10-bit, 5 or 8 channels, 7 conversion clock selects communications psp, ausart, mssp (spi and i 2 c) psp, ausart, mssp (spi and i 2 c) code protection by 8k block with separate 512-byte boot block; protection from external reads and writes, table read and intra-block table read by 8k block with separate 512-byte boot block; protection from external reads and writes, table read and intra- block table read; block 3 not protected on pic18fx539 resource type item(s) i/o resources rc1; rc2; t1oso; t1osi registers ccp1con; ccp2con; ccpr1l; ccpr2l; tmr2; pr2; t2con; osccon sfr bits ccp1ie; ccp1if; ccp1ip; ccp21e; ccp21f; ccp2ip; t1oscen; t3ccp1; tmr2on; toutps<3:0>; t2ckps<1:0>; t3ccp2; sfs; rc1; rc2; trisc1; trisc2; latc1; latc2 interrupts and interrupt resources ccp1 capture/compare match; ccp2 capture/compare match; high priority interrupts (when motor control is used; reserved for timer2) timer resources timer2 (available only through the motor control kernel); timer2 as a clock source for mssp module (spi mode) ccp resources capture and compare functionality; timer1 reset on special event; timer3 reset on special event; a/d conversion on special event; interrupt on special event configuration word bits oscen; ccp2mx; cp3; wrt3; ebtr3
? 2002 microchip technology inc. preliminary ds30485a-page 307 pic18fxx39 appendix d: migration from high-end to enhanced devices a detailed discussion of the migration pathway and dif- ferences between the high-end mcu devices (i.e., pic17cxxx) and the enhanced devices (i.e., pic18fxxx) is provided in an726, ?pic17cxxx to pic18cxxx migration?. this application note is available as literature number ds00726.
pic18fxx39 ds30485a-page 308 preliminary ? 2002 microchip technology inc. notes:
? 2002 microchip technology inc. preliminary ds30485a-page 309 pic18fxx39 index a a/d ................................................................................... 181 a/d converter flag (adif bit) ................................. 183 a/d converter interrupt, configuring ....................... 184 acquisition requirements ........................................ 184 adcon0 register .................................................... 181 adcon1 register .................................................... 181 adresh register .................................................... 181 adresh/adresl registers .................................. 183 adresl register .................................................... 181 analog port pins .................................................. 95 , 96 analog port pins, configuring .................................. 186 associated registers ............................................... 188 configuring the module ............................................ 184 conversion clock (t ad ) ........................................... 186 conversion status (go/done bit) .......................... 183 conversions ............................................................. 187 converter characteristics ........................................ 285 equations acquisition time ............................................... 185 minimum charging time .................................. 185 examples calculating the minimum required acquisition time ...................................... 185 result registers ....................................................... 187 t ad vs. device operating frequencies .................... 186 absolute maximum ratings ............................................. 259 ac (timing) characteristics ............................................. 268 conditions ................................................................ 269 load conditions for device timing specifications ....................................... 269 parameter symbology ............................................. 268 temperature and voltage specifications ................. 269 ackstat status flag ..................................................... 155 adcon0 register ............................................................ 181 go/done bit ........................................................... 183 adcon1 register ............................................................ 181 addlw ............................................................................ 217 addressable universal synchronous asynchronous receiver transmitter. see usart addwf ............................................................................ 217 addwfc ......................................................................... 218 adresh register ............................................................ 181 adresh/adresl registers ........................................... 183 adresl register ............................................................ 181 analog-to-digital converter. see a/d andlw ............................................................................ 218 andwf ............................................................................ 219 assembler mpasm assembler .................................................. 253 b baud rate generator ....................................................... 151 bc .................................................................................... 219 bcf .................................................................................. 220 bf status flag ................................................................. 155 block diagrams a/d converter .......................................................... 183 analog input model .................................................. 184 baud rate generator .............................................. 151 low voltage detect external reference source ............................. 190 internal reference source ............................... 190 mssp (i 2 c mode) .................................................... 134 mssp (spi mode) ................................................... 125 on-chip reset circuit ................................................ 23 pic18f2x39 ................................................................ 9 pic18f4x39 .............................................................. 10 pll ............................................................................ 21 portc (peripheral output override) ........................ 89 portd (i/o mode) .................................................... 91 portd and porte (parallel slave port) ................. 96 porte (i/o port mode) ............................................. 93 pwm operation (simplified) .................................... 123 ra3:ra0 and ra5 pins ............................................. 83 ra4/t0cki pin .......................................................... 84 ra6 pin ..................................................................... 84 rb2:rb0 pins ............................................................ 87 rb3 pin ..................................................................... 87 rb7:rb4 pins ............................................................ 86 reads from flash program memory ....................... 55 table read operation ............................................... 51 table write operation ................................................ 52 table writes to flash program memory ................. 57 timer0 in 16-bit mode .............................................. 100 timer0 in 8-bit mode ................................................ 100 timer1 ..................................................................... 104 timer1 (16-bit r/w mode) ....................................... 104 timer2 ..................................................................... 107 timer3 ..................................................................... 110 timer3 (16-bit r/w mode) ....................................... 110 typical motor control system .................................. 113 usart receive ....................................................... 174 usart transmit ...................................................... 172 watchdog timer ...................................................... 204 bn .................................................................................... 220 bnc ................................................................................. 221 bnn ................................................................................. 221 bnov ............................................................................... 222 bnz .................................................................................. 222 bor. see brown-out reset bov ................................................................................. 225 bra ................................................................................. 223 brg. see baud rate generator brown-out reset (bor) ..................................................... 24 bsf .................................................................................. 223 btfsc ............................................................................. 224 btfss ............................................................................. 224 btg ................................................................................. 225 bz .................................................................................... 226
pic18fxx39 ds30485a-page 310 preliminary ? 2002 microchip technology inc. c call ................................................................................ 226 clocking scheme/instruction cycle .................................... 36 clrf ................................................................................ 227 clrwdt .......................................................................... 227 code examples 16 x 16 signed multiply routine ................................. 68 16 x 16 unsigned multiply routine ............................. 68 8 x 8 signed multiply routine ..................................... 67 8 x 8 unsigned multiply routine ................................. 67 data eeprom read ................................................. 63 data eeprom refresh routine ................................ 64 data eeprom write .................................................. 63 erasing a flash program memory row .................. 56 how to clear ram (bank 1) using indirect addressing ............................................ 47 initializing porta ...................................................... 83 initializing portb ...................................................... 86 initializing portc ...................................................... 89 initializing portd ...................................................... 91 initializing porte ...................................................... 93 loading the sspbuf (sspsr) register ................. 128 motor control routine using prompt apis .............. 121 reading a flash program memory word ................ 55 saving status, wreg and bsr registers in ram ....................................... 81 writing to flash program memory ..................... 58 ? 59 code protection ............................................................... 195 comf ............................................................................... 228 configuration bits ............................................................. 195 context saving during interrupts ....................................... 81 conversion considerations .............................................. 306 cpfseq .......................................................................... 228 cpfsgt ........................................................................... 229 cpfslt ........................................................................... 229 d data eeprom memory associated registers ................................................. 65 eeadr register ........................................................ 61 eecon1 register ...................................................... 61 eecon2 register ...................................................... 61 operation during code protect .................................. 64 protection against spurious write ............................. 64 reading ...................................................................... 63 using .......................................................................... 64 write verify ................................................................. 64 writing ........................................................................ 63 data memory ...................................................................... 39 general purpose registers ........................................ 39 map for pic18fx439 ................................................. 40 map for pic18fx539 ................................................. 41 special function registers ........................................ 39 daw ................................................................................. 230 dc and ac characteristics graphs and tables ................................................... 287 dc characteristics ................................................... 261 , 264 dcfsnz ........................................................................... 231 decf ............................................................................... 230 decfsz ........................................................................... 231 developing applications ................................................... 121 development support ...................................................... 253 device differences ........................................................... 305 device overview .................................................................. 7 features ....................................................................... 8 direct addressing ............................................................... 48 example ..................................................................... 46 e electrical characteristics .................................................. 259 errata ................................................................................... 5 f firmware instructions ....................................................... 211 flash program memory ................................................... 51 associated registers ................................................. 59 control registers ....................................................... 52 erase sequence ........................................................ 56 erasing ....................................................................... 56 operation during code protection ............................. 59 reading ..................................................................... 55 tablat register ....................................................... 54 table pointer ............................................................. 54 boundaries based on operation ........................ 54 table pointer boundaries .......................................... 54 table reads and table writes .................................. 51 writing to .................................................................... 57 protection against spurious writes ................... 59 unexpected termination .................................... 59 write verify ........................................................ 59 g goto .............................................................................. 232 h hardware interface .......................................................... 113 hardware multiplier ............................................................ 67 introduction ................................................................ 67 operation ................................................................... 67 performance comparison .......................................... 67 hs/pll .............................................................................. 20 i i/o ports ............................................................................. 83 i 2 c mode bus collision during a stop condition ................................ 163 i 2 c mode .......................................................................... 134 ack pulse ........................................................138 , 139 acknowledge sequence timing .............................. 158 baud rate generator ............................................... 151 bus collision repeated start condition ............................ 162 start condition ............................................. 160 clock arbitration ...................................................... 152 clock stretching ....................................................... 144 effect of a reset .................................................... 159 general call address support ................................. 148 master mode ............................................................ 149 operation ......................................................... 150 reception ........................................................ 155 repeated start condition timing ................ 154 start condition timing ................................. 153 transmission ................................................... 155 multi-master communication, bus collision and arbitration ................................................. 159
? 2002 microchip technology inc. preliminary ds30485a-page 311 pic18fxx39 multi-master mode ................................................... 159 operation ................................................................. 138 read/write bit information (r/w bit) ............... 138 , 139 registers .................................................................. 134 serial clock (rc3/sck/scl) ................................... 139 slave mode .............................................................. 138 addressing ....................................................... 138 reception ......................................................... 139 transmission .................................................... 139 sleep operation ..................................................... 159 stop condition timing ........................................... 158 icepic in-circuit emulator .............................................. 254 id locations ............................................................. 195 , 210 incf ................................................................................. 232 incfsz ............................................................................ 233 in-circuit debugger .......................................................... 210 in-circuit serial programming (icsp) ...................... 195 , 210 indirect addressing ............................................................ 48 indf and fsr registers ........................................... 47 operation ................................................................... 47 indirect addressing operation ............................................ 48 indirect file operand .......................................................... 39 infsnz ............................................................................ 233 instruction cycle ................................................................. 36 instruction flow/pipelining ................................................. 37 instruction format ............................................................ 213 instruction set .................................................................. 211 addlw .................................................................... 217 addwf .................................................................... 217 addwfc ................................................................. 218 andlw .................................................................... 218 andwf .................................................................... 219 bc ............................................................................ 219 bcf .......................................................................... 220 bn ............................................................................ 220 bnc ......................................................................... 221 bnn ......................................................................... 221 bnov ....................................................................... 222 bnz .......................................................................... 222 bov ......................................................................... 225 bra .......................................................................... 223 bsf .......................................................................... 223 btfsc ..................................................................... 224 btfss ..................................................................... 224 btg .......................................................................... 225 bz ............................................................................ 226 call ........................................................................ 226 clrf ........................................................................ 227 clrwdt .................................................................. 227 comf ...................................................................... 228 cpfseq .................................................................. 228 cpfsgt .................................................................. 229 cpfslt ................................................................... 229 daw ......................................................................... 230 dcfsnz .................................................................. 231 decf ....................................................................... 230 decfsz ................................................................... 231 goto ...................................................................... 232 incf ......................................................................... 232 incfsz .................................................................... 233 infsnz .................................................................... 233 iorlw ..................................................................... 234 iorwf ..................................................................... 234 lfsr ........................................................................ 235 movf ....................................................................... 235 movff .................................................................... 236 movlb .................................................................... 236 movlw ................................................................... 237 movwf ................................................................... 237 mullw .................................................................... 238 mulwf .................................................................... 238 negf ....................................................................... 239 nop ......................................................................... 239 pop ......................................................................... 240 push ....................................................................... 240 rcall ..................................................................... 241 reset ..................................................................... 241 retfie .................................................................... 242 retlw .................................................................... 242 return .................................................................. 243 rlcf ....................................................................... 243 rlncf ..................................................................... 244 rrcf ....................................................................... 244 rrncf .................................................................... 245 setf ....................................................................... 245 sleep ..................................................................... 246 subfwb ................................................................. 246 sublw .................................................................... 247 subwf .................................................................... 247 subwfb ................................................................. 248 swapf .................................................................... 248 tblrd ..................................................................... 249 tblwt .................................................................... 250 tstfsz ................................................................... 251 xorlw ................................................................... 251 xorwf ................................................................... 252 summary table ....................................................... 214 instructions in program memory ........................................ 37 two-word instructions ............................................... 38 int interrupt (rb0/int). see interrupt sources intcon register rbif bit ..................................................................... 86 intcon registers ........................................................71 ? 73 inter-integrated circuit. see i 2 c interrupt sources ............................................................. 195 a/d conversion complete ....................................... 184 int0 ........................................................................... 81 interrupt-on-change (rb7:rb4) ................................ 86 portb, interrupt-on-change .................................... 81 rb0/int pin, external ................................................ 81 tmr0 ......................................................................... 81 tmr0 overflow ........................................................ 101 tmr1 overflow .................................................103 , 105 tmr2 to pr2 match (pwm) .................................... 123 tmr3 overflow .................................................109 , 111 usart receive/transmit complete ....................... 165 interrupts ............................................................................ 69 logic .......................................................................... 70 interrupts, flag bits a/d converter flag (adif bit) ................................. 183 interrupt-on-change (rb7:rb4) flag (rbif bit) ........................................................... 86 iorlw ............................................................................. 234 iorwf ............................................................................. 234 ipr registers ................................................................78 ? 79 k k ee l oq evaluation and programming tools ................... 256
pic18fxx39 ds30485a-page 312 preliminary ? 2002 microchip technology inc. l lfsr ................................................................................ 235 lookup tables computed goto ....................................................... 38 table reads, table writes ......................................... 38 low voltage detect .......................................................... 189 characteristics ......................................................... 266 effects of a reset .................................................. 193 operation ................................................................. 192 current consumption ....................................... 193 during sleep .................................................. 193 reference voltage set point ............................ 193 typical application ................................................... 189 lvd. see low voltage detect. ......................................... 189 m master ssp (mssp) module overview .................................................................. 125 master synchronous serial port (mssp). see mssp. master synchronous serial port. see mssp memory organization data memory .............................................................. 39 program memory ....................................................... 33 memory programming requirements .............................. 267 migration from high-end to enhanced devices ............... 307 motor control ........................................................... 113 , 121 prompt api methods ...................................... 117 ? 120 defined parameters ......................................... 121 software interface .................................................... 114 theory of operation ................................................. 113 v/f curve ................................................................. 114 movf ............................................................................... 235 movff ............................................................................. 236 movlb ............................................................................. 236 movlw ............................................................................ 237 movwf ........................................................................... 237 mplab c17 and mplab c18 c compilers ..................... 253 mplab icd in-circuit debugger ...................................... 255 mplab ice high performance universal in-circuit emulator with mplab ide ....................................... 254 mplab integrated development environment software .............................................. 253 mplink object linker/mplib object librarian ............... 254 mssp control registers (general) ...................................... 125 enabling spi i/o ....................................................... 129 i 2 c mode. see i 2 c ................................................... 125 operation ................................................................. 128 spi master mode ..................................................... 130 spi master/slave connection .................................. 129 spi mode ................................................................. 125 spi slave mode ....................................................... 131 sspbuf register .................................................... 130 sspsr register ....................................................... 130 typical connection ................................................... 129 mullw ............................................................................ 238 mulwf ............................................................................ 238 n negf ............................................................................... 239 nop ................................................................................. 239 o opcode field descriptions ............................................... 212 option_reg register psa bit .................................................................... 101 t0cs bit .................................................................. 101 t0ps2:t0ps0 bits ................................................... 101 t0se bit ................................................................... 101 oscillator configuration ...................................................... 19 ec .............................................................................. 19 ecio .......................................................................... 19 hs .............................................................................. 19 hs + pll ................................................................... 19 oscillator selection .......................................................... 195 oscillator, timer1 ............................................................. 103 oscillator, timer3 ............................................................. 109 oscillator, wdt ................................................................ 203 p packaging ........................................................................ 297 details ...................................................................... 299 marking information ................................................. 297 parallel slave port (psp) ..............................................91 , 96 associated registers ................................................. 97 portd ...................................................................... 96 re0/an5/rd pin ....................................................... 95 re1/an6/wr pin ..................................................95 , 96 re2/an7/cs pin ...................................................95 , 96 select (pspmode bit) .........................................91 , 96 pic18f2x39 pin functions mclr /v pp ................................................................. 11 osc1/clki ................................................................ 11 osc2/clko/ra6 ...................................................... 11 pwm1 ........................................................................ 13 pwm2 ........................................................................ 13 ra0/an0 .................................................................... 11 ra1/an1 .................................................................... 11 ra2/an2/v ref - ......................................................... 11 ra3/an3/v ref + ......................................................... 11 ra4/t0cki ................................................................. 11 ra5/an4/ss /lvdin .................................................. 11 rb0/int0 ................................................................... 12 rb1/int1 ................................................................... 12 rb2/int2 ................................................................... 12 rb3 ............................................................................ 12 rb4 ............................................................................ 12 rb5/pgm ................................................................... 12 rb6/pgc ................................................................... 12 rb7/pgd ................................................................... 12 rc0/t13cki .............................................................. 13 rc3/sck/scl ........................................................... 13 rc4/sdi/sda ............................................................ 13 rc5/sdo ................................................................... 13 rc6/tx/ck ................................................................ 13 rc7/rx/dt ................................................................ 13 v dd ............................................................................ 13 v ss ............................................................................. 13
? 2002 microchip technology inc. preliminary ds30485a-page 313 pic18fxx39 pic18f4x39 pin functions mclr /v pp .................................................................. 14 osc1/clki ................................................................ 14 osc2/clko/ra6 ...................................................... 14 pwm1 ........................................................................ 16 pwm2 ........................................................................ 16 ra0/an0 .................................................................... 14 ra1/an1 .................................................................... 14 ra2/an2/v ref - .......................................................... 14 ra3/an3/v ref + ......................................................... 14 ra4/t0cki ................................................................. 14 ra5/an4/ss /lvdin ................................................... 14 rb0/int ..................................................................... 15 rb1/int1 ................................................................... 15 rb2/int2 ................................................................... 15 rb3 ............................................................................ 15 rb4 ............................................................................ 15 rb5/pgm ................................................................... 15 rb6/pgc ................................................................... 15 rb7/pgd ................................................................... 15 rc0/t13cki ............................................................... 16 rc3/sck/scl ........................................................... 16 rc4/sdi/sda ............................................................ 16 rc5/sdo ................................................................... 16 rc6/tx/ck ................................................................ 16 rc7/rx/dt ................................................................ 16 rd0/psp0 .................................................................. 17 rd1/psp1 .................................................................. 17 rd2/psp2 .................................................................. 17 rd3/psp3 .................................................................. 17 rd4/psp4 .................................................................. 17 rd5/psp5 .................................................................. 17 rd6/psp6 .................................................................. 17 rd7/psp7 .................................................................. 17 re0/an5/rd .............................................................. 18 re1/an6/wr ............................................................. 18 re2/an7/cs .............................................................. 18 v dd ............................................................................. 18 v ss ............................................................................. 18 pic18fxx39 voltage-frequency graph (industrial) ................................................................ 260 pic18lfxx39 voltage-frequency graph (industrial) ................................................................ 260 picdem 1 low cost picmicro demonstration board ............................................... 255 picdem 17 demonstration board ................................... 256 picdem 2 low cost pic16cxx demonstration board ............................................... 255 picdem 3 low cost pic16cxxx demonstration board ............................................... 256 picstart plus entry level development programmer ............................................................. 255 pie registers ............................................................... 76 ? 77 pinout i/o descriptions pic18f2x39 .............................................................. 11 pic18f4x39 .............................................................. 14 pir registers ............................................................... 74 ? 75 pll lock time-out ............................................................. 24 pointer, fsr ....................................................................... 47 pop .................................................................................. 240 por. see power-on reset porta associated registers ................................................. 85 lata register ........................................................... 83 porta register ........................................................ 83 trisa register .......................................................... 83 portb associated registers ................................................. 88 latb register ........................................................... 86 portb register ........................................................ 86 rb0/int pin, external ................................................ 81 rb7:rb4 interrupt-on-change flag (rbif bit) ........................................................... 86 trisb register .......................................................... 86 portc associated registers ................................................. 90 latc register ........................................................... 89 portc register ........................................................ 89 rc3/sck/scl pin ................................................... 139 rc7/rx/dt pin ........................................................ 168 trisc register ...................................................89 , 165 portd associated registers ................................................. 92 latd register ........................................................... 91 parallel slave port (psp) function ............................ 91 portd register ........................................................ 91 trisd register .......................................................... 91 porte analog port pins ...................................................95 , 96 associated registers ................................................. 95 late register ........................................................... 93 porte register ........................................................ 93 psp mode select (pspmode bit) .......................91 , 96 re0/an5/rd pin ..................................................95 , 96 re1/an6/wr pin ..................................................95 , 96 re2/an7/cs pin ...................................................95 , 96 trise register .......................................................... 93 postscaler, wdt assignment (psa bit) .............................................. 101 rate select (t0ps2:t0ps0 bits) ............................. 101 switching between timer0 and wdt ...................... 101 power-down mode. see sleep power-on reset (por) ...................................................... 24 oscillator start-up timer (ost) ................................. 24 power-up timer (pwrt) ........................................... 24 prescaler, timer0 ............................................................ 101 assignment (psa bit) .............................................. 101 rate select (t0ps2:t0ps0 bits) ............................. 101 switching between timer0 and wdt ...................... 101 prescaler, timer2 ............................................................ 124 pro mate ii universal device programmer .................. 255 product identification system .......................................... 319 program counter pcl register ............................................................. 36 pclath register ...................................................... 36 pclatu register ...................................................... 36 program memory interrupt vector .......................................................... 33 map and stack for pic18fxx39 ................................ 33 reset vector ........................................................... 33 program verification and code protection ...................... 206 associated registers ............................................... 207 configuration register ............................................. 210 data eeprom ......................................................... 210 program memory ..................................................... 208
pic18fxx39 ds30485a-page 314 preliminary ? 2002 microchip technology inc. programming, device instructions ................................... 211 psp. see parallel slave port. pulse width modulation (pwm) ....................................... 123 pulse width modulation. see pwm. push ............................................................................... 240 pwm associated registers ............................................... 124 ccpr1h:ccpr1l registers ................................... 123 duty cycle ................................................................ 124 period ....................................................................... 123 tmr2 to pr2 match ................................................. 123 q q clock ............................................................................ 124 r ram. see data memory rcall .............................................................................. 241 rcsta register spen bit .................................................................. 165 register file ....................................................................... 39 registers adcon0 (a/d control 0) ......................................... 181 adcon1 (a/d control 1) ......................................... 182 ccp1con and ccp2con (pwm control) ............. 123 config1h (configuration 1 high) .......................... 196 config2h (configuration 2 high) .......................... 197 config2l (configuration 2 low) ............................ 197 config4l (configuration 4 low) ............................ 198 config5h (configuration 5 high) .......................... 199 config5l (configuration 5 low) ............................ 199 config6h (configuration 6 high) .......................... 200 config6l (configuration 6 low) ............................ 200 config7h (configuration 7 high) .......................... 201 config7l (configuration 7 low) ............................ 201 devid1 (device id 1) .............................................. 202 devid2 (device id 2) .............................................. 202 eecon1 (data eeprom control 1) ................... 53 , 62 file summary ....................................................... 43 ? 45 intcon (interrupt control) ........................................ 71 intcon2 (interrupt control 2) ................................... 72 intcon3 (interrupt control 3) ................................... 73 ipr1 (peripheral interrupt priority 1) .......................... 78 ipr2 (peripheral interrupt priority 2) .......................... 79 lvdcon (lvd control) ........................................... 191 pie1 (peripheral interrupt enable 1) .......................... 76 pie2 (peripheral interrupt enable 2) .......................... 77 pir1 (peripheral interrupt request 1) ........................ 74 pir2 (peripheral interrupt request 2) ........................ 75 rcon (register control) ........................................... 80 rcon (reset control) ............................................. 50 rcsta (receive status and control) ...................... 167 sspcon1 (mssp control 1) spi mode ......................................................... 127 sspcon1 (mssp control 1), i 2 c mode .................. 136 sspcon2 (mssp control 2), i 2 c mode .................. 137 sspstat (mssp status) spi mode ......................................................... 126 sspstat (mssp status), i 2 c mode ....................... 135 status ..................................................................... 49 stkptr (stack pointer) ............................................ 35 t0con (timer0 control) ............................................ 99 t1con (timer 1 control) ......................................... 103 t2con (timer2 control) .......................................... 107 t3con (timer3 control) .......................................... 109 trise ......................................................................... 94 txsta (transmit status and control) ..................... 166 wdtcon (watchdog timer control) ...................... 203 reset ................................................................23 , 195 , 241 brown-out reset (bor) ........................................... 195 mclr reset (during sleep) .................................... 23 mclr reset (normal operation) .............................. 23 oscillator start-up timer (ost) ............................... 195 power-on reset (por) .......................................23 , 195 power-up timer (pwrt) ......................................... 195 programmable brown-out reset (bor) .................... 23 reset instruction ..................................................... 23 stack full reset ......................................................... 23 stack underflow reset .............................................. 23 watchdog timer (wdt) reset .................................. 23 retfie ............................................................................ 242 retlw ............................................................................ 242 return .......................................................................... 243 return address stack ........................................................ 34 associated registers ................................................. 35 pointer (stkptr) ...................................................... 34 top-of-stack access .................................................. 34 revision history ............................................................... 305 rlcf ............................................................................... 243 rlncf ............................................................................. 244 rrcf ............................................................................... 244 rrncf ............................................................................ 245 s sci. see usart sck ................................................................................. 125 sdi ................................................................................... 125 sdo ................................................................................. 125 serial clock, sck ............................................................ 125 serial communication interface. see usart serial data in, sdi ........................................................... 125 serial data out, sdo ....................................................... 125 serial peripheral interface. see spi mode setf ................................................................................ 245 single phase induction motor control module. see motor control. ................................................... 113 slave select synchronization .......................................... 131 slave select, ss .............................................................. 125 sleep ..............................................................195 , 205 , 246 software simulator (mplab sim) .................................... 254 special features of the cpu ........................................... 195 configuration registers ....................................196 ? 201 special function registers ................................................ 39 map ............................................................................ 42 spi mode associated registers ............................................... 133 bus mode compatibility ........................................... 133 effects of a reset .................................................. 133 master mode ............................................................ 130 master/slave connection ......................................... 129 overview .................................................................. 125 serial clock .............................................................. 125 serial data in ........................................................... 125 serial data out ........................................................ 125 slave mode .............................................................. 131 slave select ............................................................. 125 slave select synchronization .................................. 131 slave synch timing ................................................. 131 sleep operation .................................................... 133 spi clock ................................................................. 130 ss .................................................................................... 125 sspov status flag ......................................................... 155
? 2002 microchip technology inc. preliminary ds30485a-page 315 pic18fxx39 sspstat register r/w bit ............................................................. 138 , 139 status bits significance and the initialization condition for rcon register ............................................. 25 subfwb .......................................................................... 246 sublw ............................................................................ 247 subwf ............................................................................ 247 subwfb .......................................................................... 248 swapf ............................................................................ 248 t tablat register ............................................................... 54 table pointer operations (table) ........................................ 54 tblptr register ............................................................... 54 tblrd ............................................................................. 249 tblwt ............................................................................. 250 time-out sequence ............................................................ 24 time-out in various sitations ..................................... 25 timer0 ................................................................................ 99 16-bit mode timer reads and writes ...................... 101 associated registers ............................................... 101 clock source edge select (t0se bit) ...................... 101 clock source select (t0cs bit) ............................... 101 operation ................................................................. 101 overflow interrupt .................................................... 101 prescaler. see prescaler, timer0 timer1 .............................................................................. 103 16-bit read/write mode ........................................... 105 associated registers ............................................... 105 operation ................................................................. 104 oscillator .................................................................. 103 overflow interrupt ............................................ 103 , 105 tmr1h register ...................................................... 103 tmr1l register ....................................................... 103 timer2 .............................................................................. 107 tmr2 to pr2 match interrupt .................................. 123 timer3 .............................................................................. 109 associated registers ............................................... 111 operation ................................................................. 110 oscillator .................................................................. 109 overflow interrupt ............................................ 109 , 111 tmr3h register ...................................................... 109 tmr3l register ....................................................... 109 timing diagrams a/d conversion ........................................................ 285 acknowledge sequence .......................................... 158 asynchronous reception ......................................... 175 asynchronous transmission .................................... 173 asynchronous transmission (back to back) ........... 173 baud rate generator with clock arbitration ............ 152 brg reset due to sda arbitration during start condition ................................. 161 brown-out reset (bor) ........................................... 272 bus collision during a stop condition (case 1) ........................................................... 163 bus collision during a stop condition (case 2) ........................................................... 163 bus collision during repeated start condition (case 1) ........................................... 162 bus collision during repeated start condition (case 2) ........................................... 162 bus collision during start condition (scl = 0) ......................................................... 161 bus collision during start condition (sda only) ....................................................... 160 bus collision for transmit and acknowledge ........... 159 clko and i/o .......................................................... 271 clock synchronization ............................................. 145 clock/instruction cycle .............................................. 36 example spi master mode (cke = 0) ..................... 276 example spi master mode (cke = 1) ..................... 277 example spi slave mode (cke = 0) ....................... 278 example spi slave mode (cke = 1) ....................... 279 external clock (all modes except pll) ................... 270 first start bit timing ............................................ 153 i 2 c bus data ............................................................ 280 i 2 c bus start/stop bits ...................................... 280 i 2 c master mode (7 or 10-bit transmission) ............ 156 i 2 c master mode (7-bit reception) .......................... 157 i 2 c slave mode (10-bit transmission) ..................... 143 i 2 c slave mode (7-bit transmission) ....................... 141 i 2 c slave mode with sen = 0 (10-bit reception) ............................................ 142 i 2 c slave mode with sen = 0 (7-bit reception) .............................................. 140 i 2 c slave mode with sen = 1 (10-bit reception) ............................................ 147 i 2 c slave mode with sen = 1 (7-bit reception) .............................................. 146 low voltage detect ................................................. 192 master ssp i 2 c bus data ........................................ 282 master ssp i 2 c bus start/stop bits .................. 282 parallel slave port (pic18f4x39) ........................... 275 parallel slave port (read) ......................................... 97 parallel slave port (write) ......................................... 96 pwm (pwm1 and pwm2) ....................................... 274 pwm output ............................................................ 123 repeat start condition ........................................ 154 reset, watchdog timer (wdt), oscillator start-up timer (ost) and power-up timer (pwrt) ................................. 272 slave mode general call address sequence (7 or 10-bit address mode) .............................. 148 slave synchronization ............................................. 131 slow rise time (mclr tied to v dd ) ......................... 31 spi mode (master mode) ......................................... 130 spi mode (slave mode with cke = 0) ..................... 132 spi mode (slave mode with cke = 1) ..................... 132 stop condition receive or transmit mode .............. 158 synchronous reception (master mode, sren) ...... 178 synchronous transmission ..................................... 177 synchronous transmission (through txen) .......... 177 time-out sequence on por w/pll enabled (mclr tied to v dd ) .......................................... 31 time-out sequence on power-up (mclr not tied to v dd ) case 1 ............................................................... 30 case 2 ............................................................... 30 time-out sequence on power-up (mclr tied to v dd ) .......................................... 30 timer0 and timer1 external clock .......................... 273 usart synchronous receive (master/slave) ........ 284 usart synchronous transmission (master/slave) ................................................. 284 wake-up from sleep via interrupt .......................... 206 timing diagrams requirements master ssp i 2 c bus start/stop bits .................. 282
pic18fxx39 ds30485a-page 316 preliminary ? 2002 microchip technology inc. timing requirements a/d conversion ........................................................ 286 clko and i/o ........................................................... 271 example spi mode (master mode, cke = 0) .......... 276 example spi mode (master mode, cke = 1) .......... 277 example spi mode (slave mode, cke = 0) ............ 278 example spi slave mode (cke = 1) ....................... 279 external clock .......................................................... 270 i 2 c bus data (slave mode) ...................................... 281 master ssp i 2 c bus data ........................................ 283 parallel slave port (pic18f4x39) ............................ 275 pwm ........................................................................ 274 reset, watchdog timer, oscillator start-up timer, power-up timer and brown-out reset requirements ....................... 272 timer0 and timer1 external clock ........................... 273 usart synchronous receive ................................. 284 usart synchronous transmission ......................... 284 timing specifications pll clock ................................................................. 270 trise register pspmode bit ...................................................... 91 , 96 tstfsz ............................................................................ 251 two-word instructions example cases .......................................................... 38 txsta register brgh bit .................................................................. 168 u usart ............................................................................. 165 asynchronous mode ................................................ 172 associated registers, receive ........................ 175 associated registers, transmit ....................... 173 receiver ........................................................... 174 transmitter ....................................................... 172 baud rate generator (brg) .................................... 168 associated registers ....................................... 168 baud rate error, calculating ........................... 168 baud rate formula .......................................... 168 baud rates for asynchronous mode (brgh = 0) .............................................. 170 baud rates for asynchronous mode (brgh = 1) .............................................. 171 baud rates for synchronous mode ................. 169 high baud rate select (brgh bit) .................. 168 sampling .......................................................... 168 serial port enable (spen bit) .................................. 165 synchronous master mode ...................................... 176 associated registers, reception ..................... 178 associated registers, transmit ....................... 176 reception ......................................................... 178 transmission .................................................... 176 synchronous slave mode ........................................ 179 associated registers, receive ........................ 180 associated registers, transmit ....................... 179 reception ......................................................... 180 transmission .................................................... 179 w wake-up from sleep ...............................................195 , 205 using interrupts ....................................................... 205 watchdog timer (wdt) ............................................195 , 203 associated registers ............................................... 204 control register ....................................................... 203 postscaler .........................................................203 , 204 programming considerations .................................. 203 rc oscillator ............................................................ 203 time-out period ....................................................... 203 wcol .............................................................................. 153 wcol status flag ............................................153 , 155 , 158 www, on-line support ...................................................... 5 x xorlw ............................................................................ 251 xorwf ........................................................................... 252
? 2002 microchip technology inc. preliminary ds30485a-page 317 pic18fxx39 on-line support microchip provides on-line support on the microchip world wide web site. the web site is used by microchip as a means to make files and information easily available to customers. to view the site, the user must have access to the internet and a web browser, such as netscape ? or microsoft ? internet explorer. files are also available for ftp download from our ftp site. connecting to the microchip internet web site the microchip web site is available at the following url: www.microchip.com the file transfer site is available by using an ftp ser- vice to connect to: ftp://ftp.microchip.com the web site and file transfer site provide a variety of services. users may download files for the latest development tools, data sheets, application notes, user's guides, articles and sample programs. a vari- ety of microchip specific business information is also available, including listings of microchip sales offices, distributors and factory representatives. other data available for consideration is: ? latest microchip press releases ? technical support section with frequently asked questions ? design tips ? device errata ? job postings ? microchip consultant program member listing ? links to other useful web sites related to microchip products ? conferences for products, development systems, technical information and more ? listing of seminars and events systems information and upgrade hot line the systems information and upgrade line provides system users a listing of the latest versions of all of microchip's development systems software products. plus, this line provides information on how customers can receive the most current upgrade kits.the hot line numbers are: 1-800-755-2345 for u.s. and most of canada, and 1-480-792-7302 for the rest of the world. 092002
pic18fxx39 ds30485a-page 318 preliminary ? 2002 microchip technology inc. reader response it is our intention to provide you with the best documentation possible to ensure successful use of your microchip prod- uct. if you wish to provide your comments on organization, clarity, subject matter, and ways in which our documentation can better serve you, please fax your comments to the technical publications manager at (480) 792-4150. please list the following information, and use this outline to provide us with your comments about this document. to : technical publications manager re: reader response total pages sent ________ from: name company address city / state / zip / country telephone: (_______) _________ - _________ application (optional): would you like a reply? y n device: literature number: questions: fax: (______) _________ - _________ ds30485a pic18fxx39 1. what are the best features of this document? 2. how does this document meet your hardware and software development needs? 3. do you find the organization of this document easy to follow? if not, why? 4. what additions to the document do you think would enhance the structure and subject? 5. what deletions from the document could be made without affecting the overall usefulness? 6. is there any incorrect or misleading information (what and where)? 7. how would you improve this document?
? 2002 microchip technology inc. preliminary ds30485a-page 319 pic18fxx39 pic18fxx39 product identification system to order or obtain information, e.g., on pricing or delivery, refer to the factory or the listed sales office. sales and support part no. ? x /xx xxx pattern package temperature range device device pic18fxx39 (1) , pic18fxx39t (2) ; v dd range 4.2v to 5.5v pic18lfxx39 (1) , pic18lfxx39t (2) ; v dd range 2.0v to 5.5v temperature range i= -40 c to +85 c (industrial) e= -40 c to +125 c (extended) package ml = qfn (quad flatpack, no leads) p=pdip pt = tqfp (plastic thin quad flatpack) so = soic sp = skinny plastic dip pattern qtp, sqtp, code or special requirements (blank otherwise) examples: a) pic18lf4539 - i/p 301 = industrial temp., pdip package, extended v dd limits, qtp pattern #301. b) pic18lf2439 - i/so = industrial temp., soic package, extended v dd limits. c) pic18f4439 - e/p = extended temp., pdip package, normal v dd limits. note 1: f = standard voltage range lf = wide voltage range 2: t = in tape and reel - soic, qfn, and tqfp packages only. data sheets products supported by a preliminary data sheet may have an errata sheet describing minor operational differences and recom- mended workarounds. to determine if an errata sheet exists for a particular device, please contact one of the following: 1. your local microchip sales office 2. the microchip corporate literature center u.s. fax: (480) 792-7277 3. the microchip worldwide site (www.microchip.com) please specify which device, revision of silicon and data sheet (include literature #) you are using. new customer notification system register on our web site (www.microchip.com/cn) to receive the most current information on our products.
ds30485a-page 320 preliminary ? 2002 microchip technology inc. americas corporate office 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7200 fax: 480-792-7277 technical support: 480-792-7627 web address: http://www.microchip.com rocky mountain 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7966 fax: 480-792-4338 atlanta 3780 mansell road, suite 130 alpharetta, ga 30022 tel: 770-640-0034 fax: 770-640-0307 boston 2 lan drive, suite 120 westford, ma 01886 tel: 978-692-3848 fax: 978-692-3821 chicago 333 pierce road, suite 180 itasca, il 60143 tel: 630-285-0071 fax: 630-285-0075 dallas 4570 westgrove drive, suite 160 addison, tx 75001 tel: 972-818-7423 fax: 972-818-2924 detroit tri-atria office building 32255 northwestern highway, suite 190 farmington hills, mi 48334 tel: 248-538-2250 fax: 248-538-2260 kokomo 2767 s. albright road kokomo, indiana 46902 tel: 765-864-8360 fax: 765-864-8387 los angeles 18201 von karman, suite 1090 irvine, ca 92612 tel: 949-263-1888 fax: 949-263-1338 san jose microchip technology inc. 2107 north first street, suite 590 san jose, ca 95131 tel: 408-436-7950 fax: 408-436-7955 toronto 6285 northam drive, suite 108 mississauga, ontario l4v 1x5, canada tel: 905-673-0699 fax: 905-673-6509 asia/pacific australia microchip technology australia pty ltd suite 22, 41 rawson street epping 2121, nsw australia tel: 61-2-9868-6733 fax: 61-2-9868-6755 china - beijing microchip technology consulting (shanghai) co., ltd., beijing liaison office unit 915 bei hai wan tai bldg. no. 6 chaoyangmen beidajie beijing, 100027, no. china tel: 86-10-85282100 fax: 86-10-85282104 china - chengdu microchip technology consulting (shanghai) co., ltd., chengdu liaison office rm. 2401-2402, 24th floor, ming xing financial tower no. 88 tidu street chengdu 610016, china tel: 86-28-86766200 fax: 86-28-86766599 china - fuzhou microchip technology consulting (shanghai) co., ltd., fuzhou liaison office unit 28f, world trade plaza no. 71 wusi road fuzhou 350001, china tel: 86-591-7503506 fax: 86-591-7503521 china - hong kong sar microchip technology hongkong ltd. unit 901-6, tower 2, metroplaza 223 hing fong road kwai fong, n.t., hong kong tel: 852-2401-1200 fax: 852-2401-3431 china - shanghai microchip technology consulting (shanghai) co., ltd. room 701, bldg. b far east international plaza no. 317 xian xia road shanghai, 200051 tel: 86-21-6275-5700 fax: 86-21-6275-5060 china - shenzhen microchip technology consulting (shanghai) co., ltd., shenzhen liaison office rm. 1812, 18/f, building a, united plaza no. 5022 binhe road, futian district shenzhen 518033, china tel: 86-755-82901380 fax: 86-755-82966626 china - qingdao rm. b503, fullhope plaza, no. 12 hong kong central rd. qingdao 266071, china tel: 86-532-5027355 fax: 86-532-5027205 india microchip technology inc. india liaison office divyasree chambers 1 floor, wing a (a3/a4) no. 11, o?shaugnessey road bangalore, 560 025, india tel: 91-80-2290061 fax: 91-80-2290062 japan microchip technology japan k.k. benex s-1 6f 3-18-20, shinyokohama kohoku-ku, yokohama-shi kanagawa, 222-0033, japan tel: 81-45-471- 6166 fax: 81-45-471-6122 korea microchip technology korea 168-1, youngbo bldg. 3 floor samsung-dong, kangnam-ku seoul, korea 135-882 tel: 82-2-554-7200 fax: 82-2-558-5934 singapore microchip technology singapore pte ltd. 200 middle road #07-02 prime centre singapore, 188980 tel: 65-6334-8870 fax: 65-6334-8850 taiwan microchip technology (barbados) inc., taiwan branch 11f-3, no. 207 tung hua north road taipei, 105, taiwan tel: 886-2-2717-7175 fax: 886-2-2545-0139 europe austria microchip technology austria gmbh durisolstrasse 2 a-4600 wels austria tel: 43-7242-2244-399 fax: 43-7242-2244-393 denmark microchip technology nordic aps regus business centre lautrup hoj 1-3 ballerup dk-2750 denmark tel: 45 4420 9895 fax: 45 4420 9910 france microchip technology sarl parc d?activite du moulin de massy 43 rue du saule trapu batiment a - ler etage 91300 massy, france tel: 33-1-69-53-63-20 fax: 33-1-69-30-90-79 germany microchip technology gmbh steinheilstrasse 10 d-85737 ismaning, germany tel: 49-89-627-144 0 fax: 49-89-627-144-44 italy microchip technology srl centro direzionale colleoni palazzo taurus 1 v. le colleoni 1 20041 agrate brianza milan, italy tel: 39-039-65791-1 fax: 39-039-6899883 united kingdom microchip ltd. 505 eskdale road winnersh triangle wokingham berkshire, england rg41 5tu tel: 44 118 921 5869 fax: 44-118 921-5820 12/05/02 w orldwide s ales and s ervice


▲Up To Search▲   

 
Price & Availability of 18F4539

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X